打印
[单片机资料]

qt 实时获取时间和日期

[复制链接]
1169|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 一路向北lm 于 2024-6-20 18:35 编辑

使用qt设计一个动态时钟,涉及以下资源:
1.定时器,周期定时1S,每秒刷新界面,获取当前时间
     QTimer *timer0 = new QTimer(this);
     connect(timer0,&QTimer::timeout,this,&MainWindow::time_slot);
     timer0->start(1000);

2.获取时间和日期接口类方法,并将其转换为字符串类型
#include <QDateTime>   //添加头文件
// 获取系统当前时间
QDateTime currentDateTime = QDateTime::currentDateTime();

// Debug输出
QString currentTime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
qDebug()<<currentTime;

3.添加显示ui控件,显示日期和时间
void MainWindow:: time_slot()
{
    // 获取系统当前时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // Debug输出
    QString currentTime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    ui->lcdNumber->setDigitCount(20);
    ui->lcdNumber->display(currentTime);
}



使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

3687

帖子

74

粉丝