[单片机资料] qt 实时获取时间和日期

[复制链接]
 楼主| 一路向北lm 发表于 2024-6-18 08:46 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2024-6-20 18:35 编辑

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

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

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

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



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部