Qt-Qt使用lcdNumber显示LED数据时钟
相关资料:
https://www.baidu.com/link?url=rLDb1Ka8ErpWc0ns5BUx2FEce_pdXKgv2s4q8bbMpkMwu51BFtnYXDNqbhXwZFG_IQx9XramC0J-JZ_LtkZ_aHTahl86hFTjlZK7zAyWCCC&wd=&eqid=bffb23b9000fec3500000002620b6e5c
代码实例:
.pro
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings if you use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 # Default rules for deployment. 29 qnx: target.path = /tmp/$${TARGET}/bin 30 else: unix:!android: target.path = /opt/$${TARGET}/bin 31 !isEmpty(target.path): INSTALLS += target
main.cpp
1 #include "mainwindow.h" 2 3 #include4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include5 #include 6 #include 7 8 QT_BEGIN_NAMESPACE 9 namespace Ui { class MainWindow; } 10 QT_END_NAMESPACE 11 12 class MainWindow : public QMainWindow 13 { 14 Q_OBJECT 15 16 public: 17 MainWindow(QWidget *parent = nullptr); 18 ~MainWindow(); 19 20 private: 21 Ui::MainWindow *ui; 22 private slots: 23 void on_Timer();// 事件 24 }; 25 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 // 10 setWindowTitle(QStringLiteral("Qt使用lcdNumber显示LED数据时钟")); 11 // 定时器 12 QTimer *timer = new QTimer(this); 13 connect(timer, &QTimer::timeout, this, &MainWindow::on_Timer); 14 timer->start(500); 15 } 16 17 MainWindow::~MainWindow() 18 { 19 delete ui; 20 } 21 22 void MainWindow::on_Timer() 23 { 24 QTime time = QTime::currentTime(); 25 QString txtTime = time.toString("hh:mm:ss"); 26 ui->lcdNumber->display(txtTime); 27 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2"4.0"> 3 <class>MainWindowclass> 4 class="QMainWindow" name="MainWindow"> 5 "geometry"> 6 137 120 80 9362 10216 11"windowTitle"> 14 <string>MainWindowstring> 15 16class="QWidget" name="centralwidget"> 17 37class="QLCDNumber" name="lcdNumber"> 18 36"geometry"> 19 2620 2520 2150 22321 2381 24"frameShape"> 27 <enum>QFrame::NoFrameenum> 28 29"digitCount"> 30 328 31"segmentStyle"> 33 <enum>QLCDNumber::Flatenum> 34 35class="QMenuBar" name="menubar"> 38 47"geometry"> 39 4640 450 410 42362 4322 44class="QStatusBar" name="statusbar"/> 48 4950 51