QT5.14.2 官方例子 - Qt Widgets 6: Digital Clock(数字钟)

数字时钟的例子展示了如何使用QLCDNumber来显示具有类似lcd数字的数字。

这个示例还演示了如何使用QTimer定期更新小部件。

 

详细讲解在:

http://doc.qt.io/qt-5/qtwidgets-widgets-digitalclock-example.html

 

DigitalClock类提供了一个时钟部件,它显示时间,以一个闪烁的冒号分隔小时和分钟。我们子类化QLCDNumber并实现一个私有插槽showTime()来更新时钟显示:

class DigitalClock : public QLCDNumber
  {
      Q_OBJECT

  public:
      DigitalClock(QWidget *parent = nullptr);

  private slots:
      void showTime();
  };

DigitalClock类的实现

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

在构造函数中,我们首先更改LCD编号的外观。填充样式产生凸起的段,填充前景色(通常是黑色)。我们还设置了一个1秒计时器来跟踪当前时间,并将其超时()信号连接到私有的showTime()插槽,以便每秒钟更新一次显示。然后,调用showTime()插槽;如果没有这个调用,在显示时间之前会有一秒钟的启动延迟。

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

每当需要更新时钟显示时,就会调用showTime()插槽。

当前时间被转换为一个字符串的格式"hh:mm"。当QTime::second()是偶数时,字符串中的冒号将被替换为空格。这使得冒号每隔一秒出现或消失一次。

最后,我们调用QLCDNumber::display()来更新小部件。

 

修改:

1.增加日期,时间的详细格式显示,如2020-08-31 08:00

默认显示五个数字,所以需先设置显示数字的数量为19;

参考:http://blog.sina.com.cn/s/blog_943408170102vi57.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Command line: -prefix /home/liuyh/workspace/qt5.14.2-arm -opensource -confirm-license -release -strip -shared -xplatform linux-arm-gnueabi-g++ -optimized-qmake -c++std c++11 --rpath=no -pch -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtgamepad -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpurchasing -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtwayland -skip qtwebengine -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -make libs -make examples -nomake tools -nomake tests -gui -widgets -dbus-runtime --glib=no --iconv=no --pcre=qt --zlib=qt -no-openssl --freetype=qt --harfbuzz=qt -no-opengl -linuxfb --xcb=no -tslib --libpng=qt --libjpeg=qt --sqlite=qt -plugin-sql-sqlite -I/opt/tslib/include -L/opt/tslib/lib -recheck-all executing config test machineTuple + arm-linux-gnueabi-g++ -dumpmachine > sh: 1: arm-linux-gnueabi-g++: not found test config.qtbase.tests.machineTuple FAILED executing config test verifyspec + cd /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/config.tests/verifyspec && /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/bin/qmake "CONFIG -= qt debug_and_release app_bundle lib_bundle" "CONFIG += shared warn_off console single_arch" 'QMAKE_LIBDIR += /opt/tslib/lib' 'INCLUDEPATH += /opt/tslib/include' -early "CONFIG += cross_compile" /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec + cd /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/config.tests/verifyspec && MAKEFLAGS= /usr/bin/make clean && MAKEFLAGS= /usr/bin/make > rm -f verifyspec.o > rm -f *~ core *.core > arm-linux-gnueabi-g++ -c -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard -pipe -O2 -w -fPIC -I/home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec -I. -I/opt/tslib/include -I/home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/mkspecs/linux-arm-gnueabi-g++ -o verifyspec.o /home/liuyh/workspace/QT5.14.2/qt-everywhere-src-5.14.2/qtbase/config.tests/verifyspec/verifyspec.cpp > make:arm-linux-gnueabi-g++:命令未找到 > make: *** [Makefile:172:verifyspec.o] 错误 127
最新发布
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值