转载:N3verL4nd
qttimer.h
1 #ifndef QTTIMER_H 2 #define QTTIMER_H 3 4 #include <QDialog> 5 6 namespace Ui { 7 class qttimer; 8 } 9 10 class qttimer : public QDialog 11 { 12 Q_OBJECT 13 14 public: 15 explicit qttimer(QWidget *parent = 0); 16 ~qttimer(); 17 18 private: 19 Ui::qttimer *ui; 20 int count; 21 22 private slots: 23 void slottimedown(); 24 }; 25 26 #endif // QTTIMER_H
qttimer.cpp
1 #include "qttimer.h" 2 #include <QTime> 3 #include <QTimer> 4 #include <QLabel> 5 #include <QDateTime> 6 #include "ui_qttimer.h" 7 8 qttimer::qttimer(QWidget *parent) : count(0) 9 , QDialog(parent), 10 ui(new Ui::qttimer) 11 { 12 ui->setupUi(this); 13 QTimer *time_clock = new QTimer(this); 14 connect(time_clock, SIGNAL(timeout()), this, SLOT(slottimedown())); 15 time_clock->start(1000); 16 } 17 18 qttimer::~qttimer() 19 { 20 delete ui; 21 } 22 23 24 void qttimer::slottimedown() 25 { 26 QString value; 27 ui->timelabel->setText(value.setNum(count++)); 28 QDateTime time = QDateTime::currentDateTime(); 29 QString str = time.toString("yyyy-MM-dd dddd hh:mm:ss"); 30 ui->datelabel->setText(str); 31 }
main.cpp
1 #include "qttimer.h" 2 #include <QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 qttimer w; 8 w.show(); 9 return a.exec(); 10 }
界面很简单,一个lable,两个lineEdit,然后更改对象名即可。