C++ 如何使用Qt编程获得圆柱体效果(2D)进度条?
如何更改进度条的颜色?说40%是红色,20%是绿色,40%是黄色等等。 我尝试过使用样式表,但我发现它只适用于平面进度条,而不适用于二维进度条C++ 如何使用Qt编程获得圆柱体效果(2D)进度条?,c++,qt,qt5,qprogressbar,C++,Qt,Qt5,Qprogressbar,如何更改进度条的颜色?说40%是红色,20%是绿色,40%是黄色等等。 我尝试过使用样式表,但我发现它只适用于平面进度条,而不适用于二维进度条 为了能够产生这种效果,我们画了椭圆,为此我们可以帮助QPainterPath给出它的形式,我们将椭圆的盖子放在下面: progressbar2d.h #ifndef PROGRESSBAR2D_H #define PROGRESSBAR2D_H #include <QProgressBar> class ProgressBar2D : p
为了能够产生这种效果,我们画了椭圆,为此我们可以帮助
QPainterPath
给出它的形式,我们将椭圆的盖子放在下面:
progressbar2d.h
#ifndef PROGRESSBAR2D_H
#define PROGRESSBAR2D_H
#include <QProgressBar>
class ProgressBar2D : public QProgressBar
{
Q_OBJECT
public:
ProgressBar2D(QWidget *parent = 0);
~ProgressBar2D();
protected:
void paintEvent(QPaintEvent *);
private:
void draw(QPainter *painter, QRect rect, int w);
void drawEllipticalRectangle(QPainter *painter, QRect rect, int w);
};
#endif // PROGRESSBAR2D_H
\ifndef PROGRESSBAR2D\u H
#定义PROGRESSBAR2D_H
#包括
ProgressBar2D类:公共QProgressBar
{
Q_对象
公众:
ProgressBar2D(QWidget*parent=0);
~ProgressBar2D();
受保护的:
无效油漆事件(QPaintEvent*);
私人:
虚线图(油漆工*油漆工、油漆工、油漆工);
空抽屉矩形(QPainter*painter,QRect-rect,int-w);
};
#endif//PROGRESSBAR2D\u H
progressbar2d.cpp
#include "progressbar2d.h"
#include <QPainter>
ProgressBar2D::ProgressBar2D(QWidget *parent): QProgressBar(parent)
{
}
ProgressBar2D::~ProgressBar2D()
{
}
void ProgressBar2D::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
int borde = height()/10;
draw(&painter,
QRect(borde, borde, width()-2*borde, height()-2*borde),
2*borde);
painter.drawText(rect(), Qt::AlignCenter, QString("%1").arg(value()));
}
void ProgressBar2D::draw(QPainter *painter, QRect rect, int w)
{
int w_percentage = 2*w + value()*(rect.width()-2*w)/maximum();
painter->setBrush(QBrush(QColor("#81C253")));
drawEllipticalRectangle(painter,
QRect(rect.topLeft(), QSize(w_percentage, rect.height()) ),
w);
painter->setBrush(QBrush(QColor("#C3DDB1")));
drawEllipticalRectangle(painter,
QRect(QPoint(rect.topLeft() + QPoint(w_percentage-2*w, 0)),
QSize(rect.width()-w_percentage+2*w, rect.height()) ),
w);
painter->setBrush(QBrush(QColor("#77896C")));
painter->drawEllipse(QRect( rect.topLeft() + QPoint(rect.width()-2*w, 0) , QSize(2*w, rect.height())));
}
void ProgressBar2D::drawEllipticalRectangle(QPainter *painter, QRect rect, int w)
{
painter->translate(rect.topLeft());
QPainterPath path;
path.moveTo(w, 0);
path.arcTo(QRectF(0, 0, 2*w, rect.height()), 90, 180);
path.lineTo(rect.width()-w, rect.height());
path.arcTo(QRectF(rect.width()-2*w, 0, 2*w, rect.height()), 270, -180);
path.lineTo(w, 0);
painter->drawPath(path);
painter->translate(-rect.topLeft());
}
#include "progressbar2d.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ProgressBar2D w;
QTimer *timer = new QTimer(&w);
QObject::connect(timer, &QTimer::timeout, [&w](){
w.setValue((w.value()+1)%(1 + w.maximum()));
});
timer->start(100);
w.show();
return a.exec();
}
#包括“progressbar2d.h”
#包括
ProgressBar2D::ProgressBar2D(QWidget*父项):QProgressBar(父项)
{
}
ProgressBar2D::~ProgressBar2D()
{
}
void ProgressBar2D::paintEvent(QPaintEvent*)
{
油漆工(本);
painter.setRenderInt(QPainter::抗锯齿);
int borde=高度()/10;
绘画和绘画,
QRect(borde,borde,宽度()-2*borde,高度()-2*borde),
2*博尔德);
painter.drawText(rect(),Qt::AlignCenter,QString(“%1”).arg(value());
}
void ProgressBar2D::draw(QPainter*painter,QRect rect,int w)
{
int w_percentage=2*w+value()*(矩形宽度()-2*w)/最大值();
画家->挫折(QBrush(QColor(#81C253”));
抽屉式光学矩形(画家,
QRect(rect.topLeft(),QSize(w_percentage,rect.height()),
w) );
油漆工->镶嵌(QBrush(QColor(“C3DDB1”));
抽屉式光学矩形(画家,
QRect(QPoint(rect.topLeft()+QPoint(w_percentage-2*w,0)),
QSize(矩形宽度()-w_百分比+2*w,矩形高度()),
w) );
画家->挫折(QBrush(QColor(#77896C));
painter->drawerlipse(QRect(rect.topLeft()+QPoint(rect.width()-2*w,0),QSize(2*w,rect.height());
}
void ProgressBar2D::抽屉矩形(QPainter*painter,QRect rect,int w)
{
painter->translate(rect.topLeft());
QPainterPath路径;
路径移动到(w,0);
arcTo(QRectF(0,0,2*w,rect.height()),90180);
lineTo(rect.width()-w,rect.height());
arcTo(QRectF(矩形宽度()-2*w,0,2*w,矩形高度()),270,-180);
lineTo路径(w,0);
画师->绘图路径(路径);
画师->翻译(-rect.topLeft());
}
main.cpp
#include "progressbar2d.h"
#include <QPainter>
ProgressBar2D::ProgressBar2D(QWidget *parent): QProgressBar(parent)
{
}
ProgressBar2D::~ProgressBar2D()
{
}
void ProgressBar2D::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
int borde = height()/10;
draw(&painter,
QRect(borde, borde, width()-2*borde, height()-2*borde),
2*borde);
painter.drawText(rect(), Qt::AlignCenter, QString("%1").arg(value()));
}
void ProgressBar2D::draw(QPainter *painter, QRect rect, int w)
{
int w_percentage = 2*w + value()*(rect.width()-2*w)/maximum();
painter->setBrush(QBrush(QColor("#81C253")));
drawEllipticalRectangle(painter,
QRect(rect.topLeft(), QSize(w_percentage, rect.height()) ),
w);
painter->setBrush(QBrush(QColor("#C3DDB1")));
drawEllipticalRectangle(painter,
QRect(QPoint(rect.topLeft() + QPoint(w_percentage-2*w, 0)),
QSize(rect.width()-w_percentage+2*w, rect.height()) ),
w);
painter->setBrush(QBrush(QColor("#77896C")));
painter->drawEllipse(QRect( rect.topLeft() + QPoint(rect.width()-2*w, 0) , QSize(2*w, rect.height())));
}
void ProgressBar2D::drawEllipticalRectangle(QPainter *painter, QRect rect, int w)
{
painter->translate(rect.topLeft());
QPainterPath path;
path.moveTo(w, 0);
path.arcTo(QRectF(0, 0, 2*w, rect.height()), 90, 180);
path.lineTo(rect.width()-w, rect.height());
path.arcTo(QRectF(rect.width()-2*w, 0, 2*w, rect.height()), 270, -180);
path.lineTo(w, 0);
painter->drawPath(path);
painter->translate(-rect.topLeft());
}
#include "progressbar2d.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ProgressBar2D w;
QTimer *timer = new QTimer(&w);
QObject::connect(timer, &QTimer::timeout, [&w](){
w.setValue((w.value()+1)%(1 + w.maximum()));
});
timer->start(100);
w.show();
return a.exec();
}
#包括“progressbar2d.h”
#包括
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
进展期;
QTimer*定时器=新的QTimer(&w);
QObject::connect(计时器和QTimer::超时,[&w](){
w、 设定值((w.值()+1)%(1+w.最大值());
});
定时器->启动(100);
w、 show();
返回a.exec();
}
请将您迄今为止所做的工作张贴到“如果不是2,进度条将包含多少维度?”您是否尝试过类似不同Qt主题的内容?我认为这取决于最终用户的具体外观。@NickA他的意图似乎是2½D圆柱体演示。由于找不到键盘上的[½]键,他在标题中留下了它,这导致了混乱…@Schefd很有道理,感谢你清除了upI的迷雾。我已经连续看了两分钟,真的很催眠。现在我只想要圆柱形进度条。我不明白P@eyllanesc:如何更改窗口的大小?setFixedSize在这里不起作用哪个窗口,我不明白?@eyllanesc:我不明白你代码中的这一行,int w_percentage=2*w+value()*(rect.width()-2*w)/max(),你能告诉我吗?