1、在UI界面中添加一個(gè)Label,用以顯示
文章來源:http://www.zghlxwxcb.cn/news/detail-556927.html
2、定義一個(gè)QTimer類和一個(gè)QDateTime類獲取當(dāng)前時(shí)間
//定義一個(gè)QTimer定時(shí)器
QTimer *timer = new QTimer(this);
//獲取當(dāng)前時(shí)間
void MainWindow::slotTimerUpdate()
{
QDateTime time = QDateTime::currentDateTime();//獲取當(dāng)前日期和時(shí)間
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");//格式為年-月-日 小時(shí)-分鐘-秒 星期
ui->labDateTime->setText(str);
}
3、使用信號(hào)和槽連接
//當(dāng)timer的timeout()信號(hào)發(fā)出后,觸發(fā)slotTimerUpdate()槽函數(shù)
connect(timer,SIGNAL(timeout()),this,SLOT(slotTimerUpdate()));
4、啟動(dòng)定時(shí)器
//每隔一秒發(fā)射一次timeout信號(hào)
timer->start(1000);
5、實(shí)際結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-556927.html
到了這里,關(guān)于【QT】如何實(shí)時(shí)顯示當(dāng)前時(shí)間在UI上的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!