Qt 自定义正常异常值进度条

设置标题,当前值,标准值,然后显示:

如果当前值>标准值,则显示红色。否则显示蓝色

airprogress.ui

airprogress.h

#ifndef AIRPROGRESS_H
#define AIRPROGRESS_H

#include <QWidget>

namespace Ui {
class AirProgress;
}

class AirProgress : public QWidget
{
    Q_OBJECT

public:
    explicit AirProgress(QWidget *parent = nullptr);
    ~AirProgress();
protected:
    //void paintEvent(QPaintEvent*) override;
    bool eventFilter(QObject *watched,QEvent* event) override;
public slots:
    void setValue(double,double,QString title);
private:
    Ui::AirProgress *ui;
    double m_currentValue=1;
    double m_standardValue=2;

    void showPaint();
};

#endif // AIRPROGRESS_H

airprogress.cpp

#include "airprogress.h"
#include "ui_airprogress.h"
#include <QPainter>
AirProgress::AirProgress(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AirProgress)
{
    ui->setupUi(this);
    ui->progressWidget->installEventFilter(this);
    //setValue(90,100);
}

AirProgress::~AirProgress()
{
    delete ui;
}


void AirProgress::showPaint()
{
    QPainter painter(ui->progressWidget);
    QSize size = ui->progressWidget->size();
    if (m_currentValue > m_standardValue) {
        double standardWidth = (m_standardValue/m_currentValue)*size.width();
        double currentWidth = size.width();
        // 画standard
        QPen pen(QColor(47,68,97),1);
        painter.setPen(pen);
        painter.drawLine(QPointF(0,(1.0/7.0)*size.height()),QPointF(standardWidth,(1.0/7.0)*size.height()));
        painter.drawLine(QPointF(standardWidth,(1.0/7.0)*size.height()),QPointF(standardWidth,(6.0/7.0)*size.height()));
        painter.drawLine(QPointF(standardWidth,(6.0/7.0)*size.height()),QPointF(0,(6.0/7.0)*size.height()));
        // 画current
        painter.setPen(Qt::NoPen);
        painter.setBrush(QBrush(QColor(208,70,67)));
        painter.drawRect(QRectF(0,(2.0/7.0)*size.height(),currentWidth,(3.0/7.0)*size.height()));
    } else {
        double standardWidth = size.width();
        double currentWidth = (m_currentValue/m_standardValue)*size.width();
        // 画standard
        QPen pen(QColor(47,68,97),1);
        painter.setPen(pen);
        painter.drawLine(QPointF(0,(1.0/7.0)*size.height()),QPointF(standardWidth-2,(1.0/7.0)*size.height()));
        painter.drawLine(QPointF(standardWidth-2,(1.0/7.0)*size.height()),QPointF(standardWidth-2,(6.0/7.0)*size.height()));
        painter.drawLine(QPointF(standardWidth-2,(6.0/7.0)*size.height()),QPointF(0,(6.0/7.0)*size.height()));
        // 画current
        painter.setPen(Qt::NoPen);
        painter.setBrush(QBrush(QColor(1,136,204)));
        painter.drawRect(QRectF(0,(2.0/7.0)*size.height(),currentWidth,(3.0/7.0)*size.height()));
    }
}

void AirProgress::setValue(double d1,double d2,QString title)
{
    m_currentValue = d1;
    m_standardValue = d2;
    if (m_currentValue > m_standardValue) {
        ui->curentDataLabel->setStyleSheet("QLabel{font:20px 'Microsoft YaHei' bold;color:rgb(201,68,63);}");
        ui->standardDatalabel->setStyleSheet("QLabel{font:20px 'Microsoft YaHei' bold;color:white;}");
    } else {
        ui->curentDataLabel->setStyleSheet("QLabel{font:20px 'Microsoft YaHei' bold;color:rgb(1,136,204);}");
        ui->standardDatalabel->setStyleSheet("QLabel{font:20px 'Microsoft YaHei' bold;color:white;}");
    }
    ui->curentDataLabel->setText(QString::number(d1));
    ui->standardDatalabel->setText("/ "+QString::number(d2));
    ui->titleLabel->setText(title);
    update();
}

bool AirProgress::eventFilter(QObject *watched,QEvent* event)
{
    if (watched == ui->progressWidget && event->type() == QEvent::Paint)
    {
        showPaint();// 响应函数
    }
    return QWidget::eventFilter(watched,event);
}

 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
protected:
    bool eventFilter(QObject *watched,QEvent* event);
private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;

    void showPaint();
};

#endif // MAINWINDOW_H

 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->centralWidget->setStyleSheet("QWidget#centralWidget{background-color:rgb(2,15,32);}");
    ui->centralWidget->installEventFilter(this);
    ui->widget->setValue(90,100,QString::fromLocal8Bit("二氧化碳"));
    update();
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *watched,QEvent* event)
{
    if (watched == ui->centralWidget && event->type() == QEvent::Paint)
    {
        showPaint();// 响应函数
    }
    return QMainWindow::eventFilter(watched,event);
}

void MainWindow::showPaint()
{
    QPainter painter(ui->centralWidget);
    QFont font("Microsoft YaHei",15,QFont::Bold,true);
    font.setLetterSpacing(QFont::AbsoluteSpacing,10);// 设置字体间距
    painter.setFont(font);
    painter.setPen(Qt::blue);
    painter.drawText(120,80,QString::fromLocal8Bit("程序正在初始化..."));
}

void MainWindow::on_pushButton_clicked()
{
    ui->widget->setValue(ui->lineEdit->text().toDouble(),ui->lineEdit_2->text().toDouble(),ui->lineEdit_3->text());
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值