亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于QT實現(xiàn)自定義溫度計的示例代碼

 更新時間:2023年11月07日 14:00:41   作者:瘋狂的挖掘機  
QT原生控件沒有實現(xiàn)如儀表盤或者溫度計的控件,只好自己實現(xiàn),所以本文為大家介紹了如何利用qt實現(xiàn)自定義溫度/濕度控件,感興趣的小伙伴可以了解下

0 引入

QT原生控件沒有實現(xiàn)如儀表盤或者溫度計的控件,只好自己實現(xiàn),文章代碼部分參考引用的文章。直接上圖

圖一 帶有標尺的溫度計、濕度

圖二 溫度計、濕度

控件最核心的部分:在函數paintEvent繪制部分,如果需要動畫效果還需要加一個QPropertyAnimation ,這是最主要的,剩下的細節(jié)根據需求增加減少即可。

1、帶有標尺的溫度/濕度計控件

因為只做數據顯示用,所以只需要向控件傳數據即可。

主要功能:

1、可設置顯示范圍;

2、顯示過程中加了動畫效果;

3、背景色和前景色以及刻度尺顏色可變;

4、刻度尺間距可變,控件大小隨著QWidget適應;

1.頭文件

代碼如下(示例):

protected:
    void paintEvent(QPaintEvent *);
    void drawBg(QPainter *painter);
    void drawProgress(QPainter *painter);
    void drawRulerTop(QPainter *painter);
    void drawRulerBottom(QPainter *painter);

private:
    QPropertyAnimation *m_valueAnimation;
    double minValue;                //最小值
    double maxValue;                //最大值
    qreal value;                    //當前值
    int longStep;                   //長線條等分步長
    int shortStep;                  //短線條等分步長
    bool rulerTop;                  //刻度線在上面
    bool isAdd;                     //是否為增加,默認為的增加

    QColor bgColor;                 //背景顏色
    QColor lineColor;               //線條顏色
    QColor progressColor;           //進度顏色

public:
    qreal getValue()               const;
    void setrulerTop(bool istop);   //設定刻度線再上還是在下,默認是在上
    void setValue(qreal v);
    void setRange(int minValue, int maxValue);
    void startAnimation();
    void updateValue(double value);
    void setBgColor(const QColor &bgColor);               //設置背景顏色
    void setLineColor(const QColor &lineColor);           //設置線條顏色
    void setProgressColor(const QColor &progressColor);   //設置進度顏色

2.核心代碼

繪制部分參考引用2代碼,感謝劉典武老師的無私開源,有需要的可去做定制。

代碼如下:

void HumidityProgress::startAnimation()
{
    qreal startValue =  value;
    if(isAdd)
    {
        m_valueAnimation->setKeyValueAt(0.5, value+0.5);
        m_valueAnimation->setKeyValueAt(1, value);
        m_valueAnimation->setStartValue(startValue-0.5);
        m_valueAnimation->start();
    }
    else{
        m_valueAnimation->setKeyValueAt(0.5, value-0.5);
        m_valueAnimation->setKeyValueAt(1, value);
        m_valueAnimation->setStartValue(startValue+0.5);
        m_valueAnimation->start();
    }

}

void HumidityProgress::paintEvent(QPaintEvent *)
{
    //繪制準備工作,啟用反鋸齒
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //按照繪制順序
    drawBg(&painter);
    drawProgress(&painter);
    if (rulerTop) {
        drawRulerTop(&painter);
    } else {
        drawRulerBottom(&painter);
    }
}

void HumidityProgress::drawBg(QPainter *painter)
{
    painter->save();
    painter->setPen(lineColor);
    painter->setBrush(bgColor);
    painter->drawRect(rect());
    painter->restore();
}

void HumidityProgress::drawProgress(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(progressColor);

    double length = width();
    double increment = length / (maxValue - minValue);
    double initX = (value - minValue) * increment;

    QRect rect(0, 0, initX, height());
    painter->drawRect(rect);
    painter->restore();
}

void HumidityProgress::drawRulerTop(QPainter *painter)
{
    painter->save();
    painter->setPen(lineColor);

    double initX = 0;

    //繪制橫向標尺上部分底部線
    double initTopY = 0;
    QPointF lineTopLeftPot = QPointF(initX, initTopY);
    QPointF lineTopRightPot = QPointF(width() - initX, initTopY);
    painter->drawLine(lineTopLeftPot, lineTopRightPot);

    //繪制上部分及下部分橫向標尺刻度
    double length = width();
    //計算每一格移動多少
    double increment = length / (maxValue - minValue);
    //長線條短線條長度
    int longLineLen = 15;
    int shortLineLen = 10;

    //根據范圍值繪制刻度值及刻度值 長線條需要移動10像素 短線條需要移動5像素
    for (int i = minValue; i <= maxValue; i = i + shortStep) {
        if (i % longStep == 0) {
            QPointF topPot = QPointF(initX, initTopY);
            QPointF bottomPot = QPointF(initX, initTopY + longLineLen);
            painter->drawLine(topPot, bottomPot);

            //第一個值和最后一個值不要繪制
            if (i == minValue || i == maxValue) {
                initX += increment * shortStep;
                continue;
            }

            QString strValue = QString("%1").arg((double)i, 0, 'f', 0);
            double textWidth = fontMetrics().width(strValue);
            double textHeight = fontMetrics().height();

            QPointF textPot = QPointF(initX - textWidth / 2, initTopY + textHeight + longLineLen);
            painter->drawText(textPot, strValue);
        } else {
            if (i % (longStep / 2) == 0) {
                shortLineLen = 10;
            } else {
                shortLineLen = 6;
            }

            QPointF topPot = QPointF(initX, initTopY);
            QPointF bottomPot = QPointF(initX, initTopY + shortLineLen);
            painter->drawLine(topPot, bottomPot);
        }

        initX += increment * shortStep;
    }

    painter->restore();
}

該處使用的url網絡請求的數據。

2、豎起來的溫度/濕度計控件

因為只做數據顯示用,所以只需要向控件傳數據即可。

控件是好看,但是大小不能改變,所以這里需要自己實現(xiàn)了,源碼作者已經放上。

1.頭文件

代碼如下(示例):

#ifndef THERMOMETREDLG_H
#define THERMOMETREDLG_H
#include <QWidget>
#include <QPropertyAnimation>
#include <QPainter>
#include <QTimer>
class thermometreDlg : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(qreal value READ getValue WRITE setValue)  //聲明屬性
public:
    explicit thermometreDlg(QWidget *parent = nullptr);
    qreal getValue();
    void setValue(qreal value);
    void changeValue(qreal value);
protected:
    void paintEvent(QPaintEvent *e);
public slots:
    void startAnimation();
signals:
private:
    qreal m_value;
    qreal curValue;
    int m_width;
    QRectF m_rect;
    int maxValue, minValue;
    qreal m_radius;
    QPropertyAnimation *m_valueAnimation;
    void updateRect();
};
#endif // THERMOMETREDLG_H

2.實現(xiàn)

代碼如下(示例):

#include "thermometredlg.h"
#include <QDebug>
thermometreDlg::thermometreDlg(QWidget *parent) : QWidget(parent)
{
    m_width = 20;
    maxValue = 100;
    minValue = 0;
    m_radius = 1.05;
    m_value = 0;
    curValue = m_value;
    QTimer *at = new QTimer(this);
    at->start(1000);
    m_valueAnimation = new QPropertyAnimation(this, "value");
    m_valueAnimation->setDuration(1000);
    m_valueAnimation->setEasingCurve(QEasingCurve::OutCubic);
    m_valueAnimation->setLoopCount(1);
    connect(at, SIGNAL(timeout()), this, SLOT(startAnimation()));
}
void thermometreDlg::updateRect()
{
    m_rect.setX(0);
    m_rect.setY(20 - height()/2);
    m_rect.setWidth(m_width);
    m_rect.setHeight(height() - 40 - m_width* m_radius);
}
void thermometreDlg::setValue(qreal value)
{
    m_value = value;
    update();
}
void thermometreDlg::changeValue(qreal value)
{
    if(value > maxValue)
        value = maxValue;
    if(value < minValue)
        value = minValue;
    curValue = value;
}
qreal thermometreDlg::getValue()
{
    return m_value;
}
void thermometreDlg::paintEvent(QPaintEvent *e)
{
    updateRect();
    QPainter painter(this);
    QPen pen(Qt::black);
    painter.translate(this->width()/2, this->height()/2);  //坐標軸移動到中心點
    painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing);  // 啟用反鋸齒
    painter.save();
    //繪制上方的柱狀
    painter.fillRect(m_rect, QColor(168,200, 225));
    //繪制底部的圓
    QRectF tmpRect = QRectF(m_rect.bottomLeft(), QPointF(m_width, height()/2- m_width*m_radius));
    painter.fillRect(tmpRect, QColor(255, 0, 0));
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255, 0, 0));
    painter.drawEllipse(tmpRect.bottomLeft()+QPointF(tmpRect.width()/2, 0),m_width*m_radius, m_width*m_radius);
    painter.restore();
    //繪制刻度以及刻度值
    painter.save();
    painter.setPen(QColor(Qt::black));
    int nYCount = (maxValue - minValue)/10+1;
    qreal perHeight = (m_rect.height())/nYCount;
    for (int i=0; i<nYCount; ++i) {
        QPointF basePoint = m_rect.bottomLeft() - QPointF(0, perHeight/2) - QPointF(0, perHeight*i);
        //左側大刻度
        painter.drawLine(basePoint, basePoint+QPointF(-10, 0));
        for (int j=1; j<10; ++j) {
            if(i == nYCount -1)
                continue;
            painter.drawLine(basePoint-QPointF(0, perHeight/10*j),basePoint-QPointF(5, perHeight/10*j));
        }
        painter.drawText(basePoint+QPointF(-28, 4), QString("%1").arg(minValue+i*10));
        //右側大刻度
        basePoint = m_rect.bottomRight() - QPointF(0, perHeight/2) - QPointF(0, perHeight*i);
        painter.drawLine(basePoint, basePoint+QPointF(10, 0));
        for (int j=1; j<10; ++j) {
            if(i == nYCount -1)
                continue;
            painter.drawLine(basePoint-QPointF(0, perHeight/10*j),basePoint-QPointF(-5, perHeight/10*j));
        }
    }
    painter.restore();
    //根據值填充m_rect
    qreal h = (m_value-minValue)/(maxValue-minValue)*(m_rect.height()-perHeight);
    if(h<0)
        h = 0;
    if(h > m_rect.height())
        h = m_rect.height();
    painter.fillRect(m_rect.adjusted(0, m_rect.height()-h-perHeight/2-1 , 0, 0), QColor(255, 0, 0));
    QWidget::paintEvent(e);
}
void thermometreDlg::startAnimation()
{
    qreal startValue = getValue();
    m_valueAnimation->setKeyValueAt(0, startValue-1);
    m_valueAnimation->setKeyValueAt(0.5, curValue+1);
    m_valueAnimation->setKeyValueAt(1, curValue);
    m_valueAnimation->setStartValue(startValue-2);
    m_valueAnimation->start();
}

以上就是基于QT實現(xiàn)自定義溫度計的示例代碼的詳細內容,更多關于QT溫度計的資料請關注腳本之家其它相關文章!

相關文章

  • C語言超細致講解函數遞歸

    C語言超細致講解函數遞歸

    程序調???的編程技巧稱為遞歸?recursion)函數??調???就是遞歸,你也可以理解成是?種嵌套結構,但遞歸分為倆部分,第?是“遞”,進?嵌套結構。第?是”歸“,最終會?步?步返回。第?次接觸遞歸都會很懵,慢慢理解這個過程就明?了
    2022-05-05
  • C/C++讀取配置文件的方式小結

    C/C++讀取配置文件的方式小結

    這篇文章主要為大家詳細介紹了C/C++中讀取配置文件的幾種常見方式,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-04-04
  • 一文詳解C語言中的switch語句和while循環(huán)

    一文詳解C語言中的switch語句和while循環(huán)

    這篇文章主要給大家詳細介紹了C語言中的switch語句和while循環(huán),文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-12-12
  • C++實現(xiàn)LeetCode(44.外卡匹配)

    C++實現(xiàn)LeetCode(44.外卡匹配)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(44.外卡匹配),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-07-07
  • C++類的大小介紹

    C++類的大小介紹

    這篇文章主要介紹了C++類的大小,在C++中,結構體和類的唯一區(qū)別就是結構體和類具有不同的默認訪問控制屬性,下面一起進入文章查看詳細內容
    2021-11-11
  • Qt實現(xiàn)線程與定時器的方法

    Qt實現(xiàn)線程與定時器的方法

    本文主要介紹了Qt實現(xiàn)線程與定時器的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C++中唯一三元運算符?:實例詳解

    C++中唯一三元運算符?:實例詳解

    這篇文章主要給大家介紹了關于C++中唯一三元運算符?:的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • C/C++ ip地址與int類型的轉換實例詳解

    C/C++ ip地址與int類型的轉換實例詳解

    這篇文章主要介紹了C/C++ ip地址與int類型的轉換實例詳解的相關資料,這里提供了實例代碼,實現(xiàn)思路及實現(xiàn)方法,需要的朋友可以參考下
    2016-12-12
  • C語言超詳細講解字符串函數和內存函數

    C語言超詳細講解字符串函數和內存函數

    這篇文章主要介紹一些c語言中常用字符串函數和內存函數的使用,字符串函數(String?processing?function)也叫字符串處理函數,指的是編程語言中用來進行字符串處理的函數
    2022-05-05
  • C語言堆棧幀的介紹與創(chuàng)建

    C語言堆棧幀的介紹與創(chuàng)建

    這篇文章主要給大家介紹了關于C語言堆棧幀的相關資料,堆棧幀 (stack frame)( 或活動記錄 (activation Tecord)) 是一塊堆棧保留區(qū)域,用于存放被傳遞的實際參數、子程序的返回值、局部變量以及被保存的寄存器,需要的朋友可以參考下
    2021-08-08

最新評論