27.QT-QProgressBar动态实现多彩进度条(详解)

Posted xiongjim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了27.QT-QProgressBar动态实现多彩进度条(详解)相关的知识,希望对你有一定的参考价值。

#include "ProgressBar.h"

ProgressBar:: ProgressBar(const QString&  fileName,QWidget *parent ) :
    QWidget(parent),
    m_bar(this),
    m_value(this),
    m_slider(this),
    m_image(fileName)
{

    m_bar.setMaximum(100);
    m_bar.setMinimum(0);
    m_bar.setValue(0);
    m_bar.setTextVisible(false);
    m_bar.setFixedHeight(20);

    m_slider.setMaximum(100);
    m_slider.setMinimum(0);
    m_slider.setValue(0);
    m_slider.setOrientation(Qt::Horizontal);

    m_value.setText(QString("%1%").arg(m_bar.value()));
    m_value.setAlignment(Qt::AlignRight|Qt::AlignVCenter);
    m_value.setMinimumWidth(40);
QHBoxLayout* hlayout=new QHBoxLayout(); hlayout->addWidget(&m_slider); hlayout->addWidget(&m_value,0,Qt::AlignRight); QVBoxLayout* vlayout=new QVBoxLayout(); vlayout->addWidget(&m_bar); vlayout->addLayout(hlayout); setLayout(vlayout); connect(&m_slider,SIGNAL(valueChanged ( int)),&m_bar,SLOT(setValue (int))); connect(&m_bar,SIGNAL(valueChanged ( int)),this,SLOT(onvalueChanged (int))); onvalueChanged(m_bar.value()); } void ProgressBar::onvalueChanged(int value) { QString qss="QProgressBar{" "border: 1px solid rgb(16, 135, 209);" "background: rgba(248,248,255,180);" "border-radius: 6px; }" "QProgressBar::chunk:enabled {" "border-radius: 4px; " "background: qlineargradient(x1:0, y1:0, x2:1, y2:0" ; double v= m_bar.maximum(); double EndColor=static_cast<double>(value)/v ; //获取比例 for(int i=0;i<100;i++) { double Current=EndColor*i/100; QRgb rgb =m_image.pixel((m_image.width()-1)*Current,m_image.height()/2); QColor c(rgb); qss.append(QString(",stop:%1 rgb(%2,%3,%4)").arg(i/100.0).arg(c.red()).arg(c.green()).arg(c.blue())); } qss.append(");}"); m_bar.setStyleSheet(qss); m_value.setText(QString("%1%").arg(m_bar.value())); }

文章来源:https://www.cnblogs.com/lifexy/p/9193109.html


以上是关于27.QT-QProgressBar动态实现多彩进度条(详解)的主要内容,如果未能解决你的问题,请参考以下文章

实现多彩线条摆出心形

实现多彩线条摆出心形

仅用 CSS 实现多彩智能的阴影

vue项目中用svg实现多彩滚动标签云

转CSS3+js实现多彩炫酷旋转圆环时钟效果

JavaScript DOM