• Qt---在QLabel上实现系统时间


    参考:http://blog.csdn.net/g457499940/article/details/11923887

    ---------------------------------------------

    From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/

    1.新建Gui工程,在主界面上添加一个标签label,并设置其显示内容为“0000-00-00 00:00:00 星期日”。

    2.在mainwindow.h中添加槽函数声明。

    private slots:

    void timerUpDate();

    3.在mainwindow.cpp中添加代码。

    添加#include <QtCore>的头文件包含,这样就包含了QtCore下的所有文件。

    构造函数里添加代码:

    QTimer *timer = new QTimer(this);

    //新建定时器

    connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));

    //关联定时器计满信号和相应的槽函数

    timer->start(1000);

    //定时器开始计时,其中1000表示1000ms即1秒

    4.然后实现更新函数。

    void MainWindow::timerUpDate()

    {

    QDateTime time = QDateTime::currentDateTime();

    //获取系统现在的时间

    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");

    //设置系统时间显示格式

    ui->label->setText(str);

    //在标签上显示时间

    }

    5.运行程序。

    ======================================================

    以下是本人自己整理的代码:

    mainwindow.h

     1 #ifndef MAINWINDOW_H 
     2 #define MAINWINDOW_H 
     3   
     4 #include <QMainWindow> 
     5   
     6 namespace Ui { 
     7     class MainWindow; 
     8 } 
     9   
    10 class MainWindow : public QMainWindow 
    11 { 
    12     Q_OBJECT 
    13   
    14 public: 
    15     explicit MainWindow(QWidget *parent = 0); 
    16     ~MainWindow(); 
    17   
    18 private: 
    19     void timerEvent(QTimerEvent *); 
    20   
    21 private: 
    22     Ui::MainWindow *ui; 
    23   
    24 private slots: 
    25     void on_btnLogin_clicked(); 
    26 #if 0 
    27     void timerUpDate(); 
    28 #endif 
    29 }; 
    30   
    31 #endif // MAINWINDOW_H

    mainwindow.cpp

     1 #include <QMessageBox> 
     2 #include <QtCore> 
     3 #include <time.h> 
     4 #include "mainwindow.h" 
     5 #include "ui_mainwindow.h" 
     6   
     7 MainWindow::MainWindow(QWidget *parent) : 
     8     QMainWindow(parent), 
     9     ui(new Ui::MainWindow) 
    10 { 
    11     ui->setupUi(this); 
    12 #if 0 
    13     QTimer *timer = new QTimer(this); 
    14     connect(timer, SIGNAL(timeout()), this, SLOT(timerUpDate())); 
    15     timer->start(1000); 
    16 #else 
    17     qsrand(time(0)); 
    18     startTimer(1000);       // 返回值为1, 即timerId 
    19     startTimer(5000);       // 返回值为2 
    20     startTimer(10000);      // 返回值为3 
    21 #endif 
    22 } 
    23   
    24 MainWindow::~MainWindow() 
    25 { 
    26     delete ui; 
    27 } 
    28   
    29 void MainWindow::on_btnLogin_clicked() 
    30 { 
    31     QMessageBox::information(this, "Caption", tr("Hello你好吗"), QMessageBox::Ok); 
    32 } 
    33   
    34 #if 0 
    35 void MainWindow::timerUpDate() 
    36 { 
    37     QDateTime time = QDateTime::currentDateTime(); 
    38     QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); 
    39     ui->lblCurDate->setText(str); 
    40 } 
    41 #else 
    42 void MainWindow::timerEvent(QTimerEvent *t) 
    43 { 
    44     switch(t->timerId()) 
    45     { 
    46     case 1: 
    47         { 
    48             QDateTime time = QDateTime::currentDateTime(); 
    49             QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); 
    50             ui->lblCurDate->setText(str); 
    51             ui->lbl1->setText(tr("每秒产生一个随机数: %1").arg(qrand() % 10)); 
    52             ui->lbl1->adjustSize(); 
    53         } 
    54         break; 
    55     case 2: 
    56         ui->lbl2->setText(tr("5秒后软件将关闭")); 
    57         ui->lbl2->adjustSize(); 
    58         break; 
    59     case 3: 
    60         qApp->quit();        // 退出系统 
    61         break; 
    62     } 
    63 } 
    64   
    65 #endif

    main.cpp

     1 #include <QtGui/QApplication> 
     2 #include <QTextCodec> 
     3 #include "mainwindow.h" 
     4   
     5 int main(int argc, char *argv[]) 
     6 { 
     7     QApplication a(argc, argv); 
     8     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); 
     9     MainWindow w; 
    10     w.show(); 
    11   
    12     return a.exec(); 
    13 } 
    14 
    15   
  • 相关阅读:
    linux最常用命令记录(一)
    2020centos解决“nginx 403 Forbidden"错误的故事
    nginx显示静态html爆502 bad gateway的错误提示
    codeigniter框架的使用感受和注意事项
    网闸
    抗DDOS防火墙
    负载均衡
    上网行为管理
    漏洞扫描系统
    网络分析系统
  • 原文地址:https://www.cnblogs.com/584709796-qq-com/p/4974966.html
Copyright © 2020-2023  润新知