Qt編程實(shí)現(xiàn)小時(shí)鐘
Hello World! 學(xué)習(xí)編程語(yǔ)言的最簡(jiǎn)單最經(jīng)典的小程序,當(dāng)然Qt也不例外。在學(xué)習(xí)畫(huà)圖時(shí),我覺(jué)得寫(xiě)個(gè)時(shí)鐘小程序也是個(gè)比較好的開(kāi)始。在之前的《Matlab及Java小時(shí)》一文中,我也從寫(xiě)時(shí)鐘程序作為學(xué)習(xí)畫(huà)圖的開(kāi)始。三者之間的不同點(diǎn)在于,matlab是通過(guò)while循環(huán)來(lái)進(jìn)行重繪,Java和Qt事件來(lái)處理。實(shí)時(shí)顯示時(shí)鐘,都是通過(guò)改變指針與坐標(biāo)軸的相對(duì)位置來(lái)實(shí)現(xiàn)的。前兩者都是改變指針,而Qt是旋轉(zhuǎn)坐標(biāo)軸。具體代碼如下:
1.widget.h文件
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; protected: void paintEvent(QPaintEvent *event);//添加重繪事件 }; #endif // WIDGET_H
2.main.cpp
#include <QtGui/QApplication> #include "widget.h" #include<QTextCodec> #include<QPainter> #include<QtGui> #include<QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); Widget w; w.show(); return a.exec(); }
3.widget.cpp
#include "widget.h" #include "ui_widget.h" #include<QPainter> #include<QtGui> #include<QDebug> #include<QFont> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QTimer *timer = new QTimer(this);//創(chuàng)建一秒定時(shí)器 connect(timer, SIGNAL(timeout()), this, SLOT(update()));//信號(hào)與槽關(guān)聯(lián) timer->start(1000); setWindowTitle(tr("我的時(shí)鐘--designed by TW")); } Widget::~Widget() { delete ui; } void Widget::paintEvent(QPaintEvent *event) { int side = qMin(width(), height());//獲取窗口的較短邊的值 QPainter painter(this);//指定畫(huà)圖設(shè)備 painter.fillRect(rect(),Qt::yellow);//設(shè)置背景色 painter.setRenderHint(QPainter::Antialiasing);//開(kāi)啟抗鋸齒 painter.translate(width() / 2, height() / 2);//將坐標(biāo)移到窗口正中心 painter.scale(side / 200.0, side / 200.0);//進(jìn)行縮放 QPen pen;//設(shè)置畫(huà)筆的,寬度,樣式,顏色 pen.setWidth(2); pen.setStyle(Qt::SolidLine); pen.setColor(Qt::red); painter.setPen(pen); for (int i = 0; i < 12; ++i) //畫(huà)表盤時(shí)針刻度 { painter.drawLine(87, 0, 95, 0); painter.rotate(30);//將坐標(biāo)進(jìn)行順時(shí)針選擇30度 } pen.setWidth(1); pen.setColor(Qt::black); painter.setPen(pen); for (int i = 0; i < 60; ++i) //畫(huà)表盤時(shí)針刻度 { if(i%5!=0) painter.drawLine(90, 0, 95, 0); painter.rotate(6); } pen.setColor(Qt::black); painter.setPen(pen); QFont font("Century"); painter.setFont(font); //標(biāo)時(shí)刻的數(shù)值 painter.drawText(-6,-75,tr("12")); painter.drawText(-3,80,tr("6")); painter.drawText(75,5,tr("3")); painter.drawText(-80,5,tr("9")); painter.drawArc(-3,-3,6,6,0,360*16);//畫(huà)中心小圓環(huán) ///////////////////////////////////// QTime time = QTime::currentTime();//獲取當(dāng)前系統(tǒng)時(shí)間 // qDebug()<<time.hour(); // qDebug()<<time.minute(); // qDebug()<<time.second(); //畫(huà)時(shí)針 painter.save();//在旋轉(zhuǎn)坐標(biāo)系前,保存原來(lái)坐標(biāo)系 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); pen.setColor(Qt::green); pen.setWidth(4); painter.setPen(pen); painter.drawLine(0,0,0,-60); painter.restore();//在旋轉(zhuǎn)坐標(biāo)系后,恢復(fù)原來(lái)坐標(biāo)系 //畫(huà)分針 painter.save(); painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); pen.setColor(Qt::blue); pen.setWidth(2); painter.setPen(pen); painter.drawLine(0,0,0,-80); painter.restore(); //畫(huà)秒針 painter.save(); painter.rotate(6.0 *time.second() ); pen.setColor(Qt::red); pen.setWidth(1); painter.setPen(pen); //秒針形狀由兩條直線和一個(gè)小圓環(huán)組成 painter.drawArc(-3,-66,6,6,0,360*16); painter.drawLine(0,20,0,-60); painter.drawLine(0,-66,0,-85); painter.restore(); }
運(yùn)行結(jié)果如下圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘
- Python+Pyqt實(shí)現(xiàn)簡(jiǎn)單GUI電子時(shí)鐘
- python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘
- Qt繪制簡(jiǎn)單時(shí)鐘
- QTimer與QTime實(shí)現(xiàn)電子時(shí)鐘
- Qt實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘
- Qt實(shí)現(xiàn)指針式時(shí)鐘 Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
- Qt設(shè)計(jì)時(shí)鐘效果
- QT實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
- QT5實(shí)現(xiàn)電子時(shí)鐘
相關(guān)文章
Qt數(shù)據(jù)庫(kù)相關(guān)應(yīng)用開(kāi)發(fā)總結(jié)
這篇文章主要為大家介紹了在Qt數(shù)據(jù)庫(kù)應(yīng)用開(kāi)發(fā)中的一些經(jīng)驗(yàn)總結(jié),以及一些組件的使用介紹。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02淺談Qt中使用CEF的幾個(gè)要點(diǎn)(Windows下)
下面小編就為大家?guī)?lái)一篇淺談Qt中使用CEF的幾個(gè)要點(diǎn)(Windows下)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08