Qt实现页面时钟显示

时间:2022-12-09 15:16:00

#include <QtWidgets>

#include "digitalclock.h"

DigitalClock::DigitalClock(QWidget *parent)
: QLCDNumber(parent)
{
setSegmentStyle(Filled);

QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &DigitalClock::showTime);
timer->start(1000);

showTime();

setWindowTitle(tr("Digital Clock"));
resize(150, 60);
}

void DigitalClock::showTime()
{
QTime time = QTime::currentTime();
QString text = time.toString("hh:mm");
if ((time.second() % 2) == 0)
text[2] = ' ';
display(text);
}

#include <QApplication>

#include "digitalclock.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DigitalClock clock;
clock.show();
return app.exec();
}

结果显示:

Qt实现页面时钟显示

Qt实现页面时钟显示