qt小例子:倒着走的时钟
效果图,显示当天剩余的时间
上代码:
使用了qt form
代码
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>myWidgetclass> <widget class="QWidget" name="myWidget"> <property name="geometry"> <rect> <x>0x> <y>0y> <width>500width> <height>106height> rect> property> <property name="windowTitle"> <string>myWidgetstring> property> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>0x> <y>-10y> <width>491width> <height>111height> rect> property> <property name="font"> <font> <family>Algerianfamily> <pointsize>72pointsize> font> property> <property name="text"> <string>99:99:99string> property> widget> widget> <resources/> <connections/> ui>
关键代码:
#include "mywidget.h" #include "ui_mywidget.h" #include#include #include myWidget::myWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::myWidget) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &myWidget::showTime); ui->label->setText("88:99:77"); timer->start(1000); showTime(); } void myWidget::showTime() { char time_str[10]={0}; int h,m,s; QTime time = QTime::currentTime(); h=23-time.hour(); m=59-time.minute(); s=59-time.second(); sprintf(time_str,"%02d:%02d:%02d",h,m,s); QString str = QString(QLatin1String(time_str)); ui->label->setText(str); } myWidget::~myWidget() { delete ui; }