一、
1.像温度计一样,动态往上,
二、
#ifndef RULERWIDGET_H #define RULERWIDGET_H #include <QObject> #include <QWidget> class RulerWidget : public QWidget { Q_OBJECT public: explicit RulerWidget(QWidget *parent = nullptr); signals: public slots: public: void setValue(double value,int speed=50); protected: void paintEvent(QPaintEvent *event) override; private: int bar_speed_; int current_ruler_value_; int ruler_step_; int set_ruler_pix_; int warning_line_loc_pix_; void drawRuler(QPainter &p); void drawBarBackground(QPainter &p); void drawBar(QPainter &p); }; #endif // RULERWIDGET_H
#include "rulerwidget.h" #include <QPainter> #include <QStyleOption> #include <thread> #include <QDebug> const int RULER_SIZE_MIN=20;//刻度值在画布上范围,最小值,单位像素 const int RULER_SIZE_MAX=420; const int RULER_SCALE_COUNT=50;//多少个刻度线,10个刻度线一个单位值 const int SCALE_LINE_LENGTH=20;//刻度线长度 const double WARNING_LINE_LOC=2.5;//警戒位置 RulerWidget::RulerWidget(QWidget *parent) : QWidget(parent) ,current_ruler_value_(RULER_SIZE_MAX) { this->resize(500,500); this->setStyleSheet("QWidget{background-color:#808080;}"); ruler_step_ = (RULER_SIZE_MAX-RULER_SIZE_MIN)/RULER_SCALE_COUNT;//刻度值0.1 多少个像素值 warning_line_loc_pix_ = RULER_SIZE_MAX-WARNING_LINE_LOC*ruler_step_*10;//刻度值转换成刻度尺上的实际像素值 } void RulerWidget::setValue(double value,int speed) { bar_speed_=speed; set_ruler_pix_=RULER_SIZE_MAX-value*ruler_step_ *10;//刻度值转换成像素值 qDebug()<<"px_value:"<<set_ruler_pix_; current_ruler_value_=RULER_SIZE_MAX; std::thread th=std::thread([&](){ while(current_ruler_value_>set_ruler_pix_) { current_ruler_value_=current_ruler_value_-1; update(); std::this_thread::sleep_for(std::chrono:: milliseconds (bar_speed_)); } }); th.detach(); } void RulerWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event) QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); drawRuler(p); drawBarBackground(p); drawBar(p); } void RulerWidget::drawRuler(QPainter &p) { int ruler_x=this->width()/2; p.setPen(QPen(Qt::white, 1, Qt::SolidLine, Qt::RoundCap)); p.drawLine(ruler_x,RULER_SIZE_MIN,ruler_x,RULER_SIZE_MAX); int scale_line_x=ruler_x-SCALE_LINE_LENGTH; //qDebug()<<"ruler_step:"<<ruler_step; int scale_line_y=0; for(int i=0;i<=RULER_SCALE_COUNT;i++) { scale_line_y=RULER_SIZE_MAX-i*ruler_step_; if(i%5==0) { p.setPen(QPen(Qt::white, 2, Qt::SolidLine, Qt::RoundCap)); p.drawLine(ruler_x,scale_line_y,scale_line_x-10,scale_line_y); p.drawText(scale_line_x-40,scale_line_y+5,QString::number((double)i/10.0,'f',1)); } else { p.setPen(QPen(Qt::white, 1, Qt::SolidLine, Qt::RoundCap)); p.drawLine(ruler_x,scale_line_y,scale_line_x,scale_line_y); } } } void RulerWidget::drawBarBackground(QPainter &p) { int ruler_x=this->width()/2; p.setPen(QPen(QColor("#7FFFD4"), 20, Qt::SolidLine, Qt::FlatCap)); p.drawLine(ruler_x+15,RULER_SIZE_MIN,ruler_x+15,RULER_SIZE_MAX); } void RulerWidget::drawBar(QPainter &p) { int ruler_x=this->width()/2; if(current_ruler_value_<warning_line_loc_pix_) { p.setPen(QPen(QColor("#32CD32"), 20, Qt::SolidLine, Qt::FlatCap)); p.drawLine(ruler_x+15,RULER_SIZE_MAX,ruler_x+15,warning_line_loc_pix_); p.setPen(QPen(QColor("#FF0000"), 20, Qt::SolidLine, Qt::FlatCap)); p.drawLine(ruler_x+15,warning_line_loc_pix_,ruler_x+15,current_ruler_value_); } else { p.setPen(QPen(QColor("#32CD32"), 20, Qt::SolidLine, Qt::FlatCap)); p.drawLine(ruler_x+15,RULER_SIZE_MAX,ruler_x+15,current_ruler_value_); } p.setPen(QPen(Qt::white, 1, Qt::SolidLine, Qt::RoundCap)); p.drawText(ruler_x+35,current_ruler_value_,50,30,Qt::AlignLeft,QString::number((RULER_SIZE_MAX-current_ruler_value_)/(ruler_step_*10.0),'f',1)); }