
类似LCD那种的吗?还是单纯的 09:55:11
如果是LCD的话,使用如下:
class QLCDNumervoid display(const QString &s)
// 如果是一般的数字显示用一个QLabel就可以了
class QLabel;void setText(const QString &text)
这两种仅仅只是显示而已,完成时间动态刷新还得靠定时器,QTimer
QTimer *timer = new QTimer()
timer->start(1000) // 每秒刷新一次
// 关联信号槽
connect(timer, SIGNAL(timerout()), this, SLOT(showTime()))
// 然后槽函数写实现和显示方法
void MainWindow::showTime() {
lcdNumer->display(QDateTime::currentDateTime().toString("hh:mm:ss))
label->setText(QDateTime::currentDateTime().toString("hh:mm:ss))
}
QDateTime就是获取系统时间的类
用一个QTimer槽连接下面的槽函数。void UiMainWindow::slotClockTimeout()
{
QString str
QDate date = QDate::currentDate()
QTime time = QTime::currentTime()
str = QString("%1-%2-%3 %4").arg(date.year()).arg(date.month()).arg(date.day()).arg(time.toString())
str = str.left(str.length() - 3)//去除秒钟的显示
ui->labelClock->setText(str)//显示时间在label上。
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)