如何使用QTimer和QTime构建一个电子时钟程序?

2026-04-29 14:526阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计508个文字,预计阅读时间需要3分钟。

如何使用QTimer和QTime构建一个电子时钟程序?

本例展示了如何使用QTimer和QTime实现电子时钟的具体代码。以下是大家的参考内容:

使用QLCDNumber控件进行显示,默认只显示5个字符,可以使用setDigitCount(int size)进行设置。

cpp#include #include #include

void setupClock(QLCDNumber *lcdNumber) { QTimer *timer=new QTimer(this); connect(timer, &QTimer::timeout, [lcdNumber]() { QTime time=QTime::currentTime(); lcdNumber->display(time.toString(hh:mm:ss)); }); timer->start(1000); // 更新频率为1秒}

如何使用QTimer和QTime构建一个电子时钟程序?

本文实例为大家分享了QTimer与QTime实现电子时钟的具体代码,供大家参考,具体内容如下

使用QLCDNumber控件进行显示

QLCDNumber控件默认只显示5个字符,可以使用setDigitCount(int size)进行设置显示个数

使用Display(QString str) 设置显示内容

该函数拥有多个重载,字符 整型 浮点型都可以作为参数

效果图:

代码:头文件

#include <QLCDNumber> class NumClock : public QLCDNumber { Q_OBJECT public: explicit NumClock(QWidget *parent = nullptr); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); signals: public slots: void updateTime(); private: QTimer * timer; QPoint mouseOfPonit; // 鼠标坐标跟窗口左上角坐标的偏移值 bool showColon; //是否显示: };

cpp文件:

#include "numclock.h" #include <QTimer> #include <QTime> #include <QMouseEvent> #include <QDebug> NumClock::NumClock(QWidget *parent) : QLCDNumber(parent) { timer = new QTimer(this); timer->setTimerType(Qt::PreciseTimer); // 设置精度为较高精度,差距在毫秒内 timer->start(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()),Qt::QueuedConnection); setWindowFlag(Qt::FramelessWindowHint); //没有面板边框标题栏的窗体 setWindowOpacity(0.5); //设置窗口的透明度 showColon = true; this->setDigitCount(8); resize(150, 100); updateTime(); setAttribute(Qt::WA_DeleteOnClose); } void NumClock::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton){ mouseOfPonit = event->globalPos() - this->pos(); event->accept(); }else{ close(); } } void NumClock::mouseMoveEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton){ move(event->globalPos() - mouseOfPonit); event->accept(); } } void NumClock::updateTime() { QString timeStr = QTime::currentTime().toString("hh:mm:ss"); if(showColon){ timeStr = timeStr.replace(QString(":"), QString(" ")); qDebug() << timeStr; showColon = false; }else{ timeStr = timeStr.replace(QString(" "), QString(":")); showColon = true; qDebug() << timeStr; } display(timeStr); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计508个文字,预计阅读时间需要3分钟。

如何使用QTimer和QTime构建一个电子时钟程序?

本例展示了如何使用QTimer和QTime实现电子时钟的具体代码。以下是大家的参考内容:

使用QLCDNumber控件进行显示,默认只显示5个字符,可以使用setDigitCount(int size)进行设置。

cpp#include #include #include

void setupClock(QLCDNumber *lcdNumber) { QTimer *timer=new QTimer(this); connect(timer, &QTimer::timeout, [lcdNumber]() { QTime time=QTime::currentTime(); lcdNumber->display(time.toString(hh:mm:ss)); }); timer->start(1000); // 更新频率为1秒}

如何使用QTimer和QTime构建一个电子时钟程序?

本文实例为大家分享了QTimer与QTime实现电子时钟的具体代码,供大家参考,具体内容如下

使用QLCDNumber控件进行显示

QLCDNumber控件默认只显示5个字符,可以使用setDigitCount(int size)进行设置显示个数

使用Display(QString str) 设置显示内容

该函数拥有多个重载,字符 整型 浮点型都可以作为参数

效果图:

代码:头文件

#include <QLCDNumber> class NumClock : public QLCDNumber { Q_OBJECT public: explicit NumClock(QWidget *parent = nullptr); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); signals: public slots: void updateTime(); private: QTimer * timer; QPoint mouseOfPonit; // 鼠标坐标跟窗口左上角坐标的偏移值 bool showColon; //是否显示: };

cpp文件:

#include "numclock.h" #include <QTimer> #include <QTime> #include <QMouseEvent> #include <QDebug> NumClock::NumClock(QWidget *parent) : QLCDNumber(parent) { timer = new QTimer(this); timer->setTimerType(Qt::PreciseTimer); // 设置精度为较高精度,差距在毫秒内 timer->start(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()),Qt::QueuedConnection); setWindowFlag(Qt::FramelessWindowHint); //没有面板边框标题栏的窗体 setWindowOpacity(0.5); //设置窗口的透明度 showColon = true; this->setDigitCount(8); resize(150, 100); updateTime(); setAttribute(Qt::WA_DeleteOnClose); } void NumClock::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton){ mouseOfPonit = event->globalPos() - this->pos(); event->accept(); }else{ close(); } } void NumClock::mouseMoveEvent(QMouseEvent *event) { if(event->buttons() & Qt::LeftButton){ move(event->globalPos() - mouseOfPonit); event->accept(); } } void NumClock::updateTime() { QString timeStr = QTime::currentTime().toString("hh:mm:ss"); if(showColon){ timeStr = timeStr.replace(QString(":"), QString(" ")); qDebug() << timeStr; showColon = false; }else{ timeStr = timeStr.replace(QString(" "), QString(":")); showColon = true; qDebug() << timeStr; } display(timeStr); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。