Qt示例教程之用Qt畫一個(gè)溫度計(jì)
示例1
以下是用Qt繪制一個(gè)簡(jiǎn)單的溫度計(jì)的示例代碼:
#include <QPainter>
#include <QWidget>
#include <QApplication>
class Thermometer : public QWidget {
public:
Thermometer(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
private:
int value;
};
Thermometer::Thermometer(QWidget *parent)
: QWidget(parent), value(50) {}
void Thermometer::paintEvent(QPaintEvent * /* event */) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
// 繪制溫度計(jì)背景
painter.setBrush(QColor("#f0f0f0"));
painter.drawRect(0, 0, width(), height());
// 繪制溫度計(jì)外框
painter.setBrush(QColor("#f5f5f5"));
painter.drawRect(20, 20, width() - 40, height() - 40);
// 繪制溫度計(jì)刻度線
painter.setPen(QColor("#999999"));
for (int i = 0; i <= 100; i += 10) {
painter.drawLine(40, height() - 40 - (i * (height() - 80) / 100),
60, height() - 40 - (i * (height() - 80) / 100));
}
// 繪制溫度計(jì)當(dāng)前溫度
painter.setPen(QColor("#ff0000"));
painter.setFont(QFont("Arial", 14));
painter.drawText(80, height() , QString::number(value) + ".");
// 繪制溫度計(jì)水銀柱
painter.setBrush(QColor("#ff0000"));
painter.drawRect(30, height() - 40 - (value * (height() - 80) / 100),
width() - 60, value * (height() - 80) / 100);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Thermometer thermometer;
thermometer.setWindowTitle("thermometer");
thermometer.resize(200, 400);
thermometer.show();
return app.exec();
}運(yùn)行結(jié)果

示例2
復(fù)雜的動(dòng)態(tài)溫度計(jì),代碼實(shí)現(xiàn)如下:
創(chuàng)建一個(gè)Qt項(xiàng)目,并添加一個(gè)主窗口。
新建Qt Widgets Application項(xiàng)目,添加MainWindow類。
在主窗口上添加一個(gè)QLabel控件,用于顯示當(dāng)前溫度值。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QLabel *tempLabel = new QLabel(this);
tempLabel->setText("Temp: 0℃");
tempLabel->setGeometry(20, 20, 100, 20);
在主窗口上添加一個(gè)QGraphicsView控件,用于繪制溫度計(jì)。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QGraphicsView *view = new QGraphicsView(this); view->setGeometry(20, 50, 200, 200);
創(chuàng)建一個(gè)QGraphicsScene對(duì)象,并將其設(shè)置為QGraphicsView的場(chǎng)景。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QGraphicsScene *scene = new QGraphicsScene(this); view->setScene(scene);
在QGraphicsScene中添加一個(gè)QGraphicsRectItem對(duì)象,用于表示溫度計(jì)的外框。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QGraphicsRectItem *frame = new QGraphicsRectItem(0, 0, 200, 200); frame->setPen(QPen(Qt::black)); frame->setBrush(QBrush(Qt::white)); scene->addItem(frame);
在QGraphicsScene中添加一個(gè)QGraphicsEllipseItem對(duì)象,用于表示溫度計(jì)的中心點(diǎn)。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QGraphicsEllipseItem *center = new QGraphicsEllipseItem(90, 90, 20, 20); center->setPen(QPen(Qt::black)); center->setBrush(QBrush(Qt::black)); scene->addItem(center);
在QGraphicsScene中添加一些QGraphicsLineItem對(duì)象,用于表示溫度計(jì)的刻度線。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
for (int i = -45; i <= 225; i += 45) {
QLineF line(100 + 80 * cos(i * M_PI / 180),
100 - 80 * sin(i * M_PI / 180),
100 + 100 * cos(i * M_PI / 180),
100 - 100 * sin(i * M_PI / 180));
QGraphicsLineItem *tick = new QGraphicsLineItem(line);
tick->setPen(QPen(Qt::black));
scene->addItem(tick);
}
在QGraphicsScene中添加一個(gè)QGraphicsPolygonItem對(duì)象,用于表示溫度計(jì)的指針。
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
QPointF points[3] = { QPointF(100, 100),
QPointF(95, 90),
QPointF(105, 90) };
QGraphicsPolygonItem *pointer = new QGraphicsPolygonItem(QPolygonF(points, 3));
pointer->setPen(QPen(Qt::black));
pointer->setBrush(QBrush(Qt::red));
pointer->setTransformOriginPoint(100, 100);
scene->addItem(pointer);
通過(guò)QGraphicsTransform對(duì)象,將溫度計(jì)的指針進(jìn)行旋轉(zhuǎn),以顯示當(dāng)前溫度值。
在MainWindow類中添加以下私有成員變量:
QGraphicsTransform *pointerTransform;
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
pointerTransform = new QGraphicsTransform(pointer); pointer->setTransform(pointerTransform);
在MainWindow類中添加以下槽函數(shù):
void MainWindow::updateTemp(int temp) {
ui->tempLabel->setText(QString("Temp: %1℃").arg(temp));
pointerTransform->setRotation(-45 + temp * 270 / 100);
}
在主窗口的槽函數(shù)中更新溫度值,并更新溫度計(jì)的指針的旋轉(zhuǎn)角度。
在MainWindow類中添加以下信號(hào):
signals:
void tempChanged(int temp);
在MainWindow類的構(gòu)造函數(shù)中添加以下代碼:
connect(this, SIGNAL(tempChanged(int)), this, SLOT(updateTemp(int)));
在MainWindow類中添加以下槽函數(shù):
void MainWindow::on_tempSlider_valueChanged(int value)
{
emit tempChanged(value);
}
在主窗口上添加一個(gè)QSlider控件,并將其范圍設(shè)置為0-100,在MainWindow類中添加以下信號(hào)槽連接:
connect(ui->tempSlider, SIGNAL(valueChanged(int)), this, SLOT(on_tempSlider_valueChanged(int)));
總結(jié)
到此這篇關(guān)于Qt示例教程之用Qt畫一個(gè)溫度計(jì)的文章就介紹到這了,更多相關(guān)Qt畫溫度計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV使用GrabCut實(shí)現(xiàn)摳圖功能
Grabcut是基于圖割(graph cut)實(shí)現(xiàn)的圖像分割算法,它需要用戶輸入一個(gè)bounding box作為分割目標(biāo)位置,實(shí)現(xiàn)對(duì)目標(biāo)與背景的分離/分割。本文將使用GrabCut實(shí)現(xiàn)摳圖功能,需要的可以參考一下2023-02-02
淺析C語(yǔ)言中strtol()函數(shù)與strtoul()函數(shù)的用法
這篇文章主要介紹了淺析C語(yǔ)言中strtol()函數(shù)與strtoul()函數(shù)的用法,注意其將字符串轉(zhuǎn)換成long型的區(qū)別,需要的朋友可以參考下2015-08-08
使用用C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼
大家好,本篇文章主要講的是使用用C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,歡迎借鑒學(xué)習(xí)C++做一顆會(huì)跳動(dòng)的愛(ài)心實(shí)例代碼2021-12-12
C++?STL之string的模擬實(shí)現(xiàn)實(shí)例代碼
C++中有命名空間的存在,我們只需把我們的代碼封到自定義的命名空間即可,下面這篇文章主要給大家介紹了關(guān)于C++?STL之string的模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-01-01
數(shù)據(jù)結(jié)構(gòu)之位圖(bitmap)詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之位圖詳解,本文講解了位圖的基本知識(shí)、位圖的實(shí)現(xiàn)方法、位圖的應(yīng)用等內(nèi)容,需要的朋友可以參考下2014-08-08
C++ BloomFilter布隆過(guò)濾器應(yīng)用及概念詳解
布隆過(guò)濾器是由布?。˙urton Howard Bloom)在1970年提出的 一種緊湊型的、比較巧妙的概率型數(shù)據(jù)結(jié)構(gòu),特點(diǎn)是高效地插入和查詢,可以用來(lái)告訴你 “某樣?xùn)|西一定不存在或者可能存在”,它是用多個(gè)哈希函數(shù),將一個(gè)數(shù)據(jù)映射到位圖結(jié)構(gòu)中2023-03-03
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單停車場(chǎng)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單停車場(chǎng)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

