參考: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
