qt小例子:倒着走的时钟

效果图,显示当天剩余的时间

 

上代码:

使用了qt form

代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>myWidget</class>
 <widget class="QWidget" name="myWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>500</width>
    <height>106</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>myWidget</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>-10</y>
     <width>491</width>
     <height>111</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Algerian</family>
     <pointsize>72</pointsize>
    </font>
   </property>
   <property name="text">
    <string>99:99:99</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

关键代码:

#include "mywidget.h"
#include "ui_mywidget.h"
#include <QTimer>
#include <QTime>
#include <stdio.h>

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;
}

 

posted @ 2020-12-10 14:55  HuDaXia  阅读(157)  评论(0编辑  收藏  举报