基于QT繪制一個(gè)漂亮的預(yù)警儀表
以下是用Qt實(shí)現(xiàn)漂亮預(yù)警儀表的步驟和代碼:
1.創(chuàng)建一個(gè)Qt項(xiàng)目,并添加一個(gè)主窗口。
2.在主窗口中添加QGraphicsView控件,用于繪制預(yù)警儀表。
3.創(chuàng)建一個(gè)QGraphicsScene對(duì)象,并將其設(shè)置為QGraphicsView的場(chǎng)景。
QGraphicsScene *scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene);
4.創(chuàng)建一個(gè)QGraphicsEllipseItem對(duì)象,用于表示預(yù)警儀表的外框。
QGraphicsEllipseItem *outerCircle = new QGraphicsEllipseItem(QRectF(-120,-120,240,240)); outerCircle->setPen(QPen(Qt::black, 4)); scene->addItem(outerCircle);
5.創(chuàng)建一個(gè)QGraphicsEllipseItem對(duì)象,用于表示預(yù)警儀表的中心點(diǎn)。
QGraphicsEllipseItem *centerCircle = new QGraphicsEllipseItem(QRectF(-15,-15,30,30)); centerCircle->setBrush(QBrush(Qt::red)); centerCircle->setPen(QPen(Qt::black, 2)); centerCircle->setZValue(1); scene->addItem(centerCircle);
6.創(chuàng)建一些QGraphicsLineItem對(duì)象,用于表示預(yù)警儀表的刻度線。
for (int i = 0; i < 12; i++) { QGraphicsLineItem *line = new QGraphicsLineItem(-100, 0, -80, 0); line->setPen(QPen(Qt::black, 3)); line->setRotation(i * 30); line->setPos(0, 0); scene->addItem(line); }
7.創(chuàng)建一個(gè)QGraphicsPolygonItem對(duì)象,用于表示預(yù)警儀表的指針。
QPolygonF polygon; polygon << QPointF(-10, 0) << QPointF(0, -120) << QPointF(10, 0); QGraphicsPolygonItem *pointer = new QGraphicsPolygonItem(polygon); pointer->setBrush(QBrush(Qt::red)); pointer->setPen(QPen(Qt::black, 2)); pointer->setZValue(2); pointer->setPos(0, 0); scene->addItem(pointer);
8.在主窗口的定時(shí)器中更新指針的旋轉(zhuǎn)角度。
void MainWindow::timerEvent(QTimerEvent *event) { Q_UNUSED(event); int angle = ui->spinBox->value(); QGraphicsItem *item = ui->graphicsView->scene()->items().at(3); item->setRotation(angle); }
完整的代碼如下:
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsScene> #include <QGraphicsEllipseItem> #include <QGraphicsPolygonItem> #include <QGraphicsLineItem> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected: void timerEvent(QTimerEvent *event); private: Ui::MainWindow *ui; QGraphicsScene *scene; QGraphicsEllipseItem *outerCircle; QGraphicsEllipseItem *centerCircle; QGraphicsPolygonItem *pointer; QList<QGraphicsLineItem*> lines; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 創(chuàng)建場(chǎng)景 scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); // 創(chuàng)建外框 outerCircle = new QGraphicsEllipseItem(QRectF(-120,-120,240,240)); outerCircle->setPen(QPen(Qt::black, 4)); scene->addItem(outerCircle); // 創(chuàng)建中心點(diǎn) centerCircle = new QGraphicsEllipseItem(QRectF(-15,-15,30,30)); centerCircle->setBrush(QBrush(Qt::red)); centerCircle->setPen(QPen(Qt::black, 2)); centerCircle->setZValue(1); scene->addItem(centerCircle); // 創(chuàng)建指針 QPolygonF polygon; polygon << QPointF(-10, 0) << QPointF(0, -120) << QPointF(10, 0); pointer = new QGraphicsPolygonItem(polygon); pointer->setBrush(QBrush(Qt::red)); pointer->setPen(QPen(Qt::black, 2)); pointer->setZValue(2); pointer->setPos(0, 0); pointer->setRotation(0); scene->addItem(pointer); // 創(chuàng)建刻度線 for (int i = 0; i < 12; i++) { QGraphicsLineItem *line = new QGraphicsLineItem(-100, 0, -80, 0); line->setPen(QPen(Qt::black, 3)); line->setRotation(i * 30); line->setPos(0, 0); lines.append(line); scene->addItem(line); } // 啟動(dòng)定時(shí)器 startTimer(100); } MainWindow::~MainWindow() { delete ui; } void MainWindow::timerEvent(QTimerEvent *event) { Q_UNUSED(event); int angle = ui->spinBox->value(); QGraphicsItem *item = ui->graphicsView->scene()->items().at(3); pointer->setRotation(angle); }
用Qt畫(huà)一個(gè)漂亮預(yù)警儀表,具有立體感,代碼,描述設(shè)計(jì)步驟
到此這篇關(guān)于基于QT繪制一個(gè)漂亮的預(yù)警儀表的文章就介紹到這了,更多相關(guān)QT預(yù)警儀表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++左值與右值,右值引用,移動(dòng)語(yǔ)義與完美轉(zhuǎn)發(fā)詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03c++ 解決無(wú)法打印uint8_t 類(lèi)型變量的問(wèn)題
這篇文章主要介紹了c++ 解決無(wú)法打印uint8_t 類(lèi)型變量的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12解析Linux內(nèi)核的基本的模塊管理與時(shí)間管理操作
這篇文章主要介紹了Linux內(nèi)核的基本的模塊管理與時(shí)間管理操作,包括模塊加載卸載函數(shù)的使用和定時(shí)器的用法等知識(shí),需要的朋友可以參考下2016-02-02C/C++?Qt?運(yùn)用JSON解析庫(kù)的實(shí)例代碼
這篇文章主要介紹了C/C++?Qt?運(yùn)用JSON解析庫(kù)的相關(guān)知識(shí),通過(guò)代碼依次解析這個(gè)json文件中的每一個(gè)參數(shù),代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01C語(yǔ)言基礎(chǔ)之二分查找知識(shí)最全匯總
這篇文章主要介紹了C語(yǔ)言基礎(chǔ)之二分查找知識(shí)最全匯總,文中有非常詳細(xì)的二分查找基礎(chǔ)知識(shí)詳解,對(duì)正在學(xué)習(xí)C語(yǔ)言基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04C++中的vector使用詳解及重要部分底層實(shí)現(xiàn)
本篇文章會(huì)對(duì)vector的語(yǔ)法使用進(jìn)行詳解,同時(shí),還會(huì)對(duì)重要難點(diǎn)部分的底層實(shí)現(xiàn)進(jìn)行講解,其中有vector的迭代器失效和深拷貝問(wèn)題,希望本篇文章的內(nèi)容會(huì)對(duì)你有所幫助2023-07-07C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
本文主要介紹了C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03