QT自制秒表计时器、可获取电脑时间
生活随笔
收集整理的這篇文章主要介紹了
QT自制秒表计时器、可获取电脑时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自制秒表
功能 :
界面
界面總共包含三種控件:
三種控件分別用于以下顯示:
3.
代碼
.h頭文件
#include <QTimer>//添加倆個頭文件,一個是定時器,一個是時間控制 #include <QTime>class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();QTime time;//添加三個對象,一個時間 ,兩個定時器QTimer timer;QTimer current_timer;private slots:void on_startBt_clicked();//開始按鈕的槽函數void timerout_slot();//兩個定時器觸發的槽函數void currenttimerout_slot();void on_stopBt_clicked();void on_chongzhiBt_clicked();void on_dadianBt_clicked();private:Ui::Widget *ui; };.cpp源文件
int jishu = 0; Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);connect(&timer, SIGNAL(timeout()), this, SLOT(timerout_slot()));//創建定時器的觸發槽函數連接,用于獲取秒表延時connect(¤t_timer, SIGNAL(timeout()), this, SLOT(currenttimerout_slot()));//創建定時器的觸發槽函數連接,用于獲取電腦時間的延時current_timer.start(1000);//1s鐘觸發一次time.setHMS(0,0,0);//設置時間ui->timeLb->setText("00:00:00:000");//顯示計時器數據 }Widget::~Widget() {delete ui; } void Widget::currenttimerout_slot()//獲取電腦時間并顯示出來 {QDateTime UTC(QDateTime::currentDateTimeUtc());QDateTime local(UTC.toLocalTime());ui->ctimeLb->setText(local.toString("yyyy.MM.dd hh:mm:ss"));} void Widget::timerout_slot()//秒表時間控制 {//qDebug("ha");time = time.addMSecs(1);ui->timeLb->setText(time.toString("hh:mm:ss.zzz")); } void Widget::on_startBt_clicked()//啟動定時器 {timer.start(1);//ms定時器}void Widget::on_stopBt_clicked() {timer.stop(); }void Widget::on_chongzhiBt_clicked()//重置清除打點數據 {time.setHMS(0, 0, 0);ui->timeLb->setText(time.toString("hh:mm:ss.zzz"));jishu = 0;ui->jiluTb->clear(); }void Widget::on_dadianBt_clicked()//打點,時間顯示 {QString timp;jishu++;timp.sprintf("%2d.%s",jishu,time.toString("hh:mm:ss.zzz").toLatin1().data());//將Qstring* --> char*ui->jiluTb->append(timp); }巧妙的代碼
使用Text Browser控件時,顯示數據是QString 類型的,使用append()對數進行追加顯示,每次追加完成之后都會進行換行,如果顯示在同一行就需要一個追加,上邊采用的代碼是:
timp.sprintf(“%2d.%s”,jishu,time.toString(“hh:mm:ss.zzz”).toLatin1().data());
QString ——>char * 類型的轉換
time.toString(“hh:mm:ss.zzz”).toLatin1().data()
time:時間的類
time.toString(“hh:mm:ss.zzz”):將時間取出來,得到指定格式的時間,是Qstring 類型
源代碼下載
https://download.csdn.net/download/qq_30255657/85795043
總結
以上是生活随笔為你收集整理的QT自制秒表计时器、可获取电脑时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客练习赛37 C 筱玛的迷阵探险(Tr
- 下一篇: UGUI 源码之 RectMask2D、