怎么用Qt Creator制作数字时钟

怎么用Qt Creator制作数字时钟,第1张

类似LCD那种的吗?还是单纯的 09:55:11

如果是LCD的话,使用如下:

class QLCDNumer

void 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就是获取系统时间的类

可以直接用QDateTime的格式化输出就行了。不用区分linux或windowsQString strDateTime = QDateTime::currentDateTime().toString("yyyy年MM月dd日 hh:mm:ss")// 然后找个label来显示这个时间就行了labelTime->setText(strDateTime

用一个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上。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11786057.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存