I'm drawing colored rectangular bars that switches from colored and non-colored states. However, it seems that when I use the QPainter::fillPath function, some of the border gets hidden by the added color. How should I alter my code in a way that the rectangle keeps its border? Is there any programmatic way of doing this besides altering manually the X and Y coordinates of the rectangle?
Here's the code that I'm using to draw the rectangles:
#include <QWidget>
#include <QPainter>
#include <QtWidgets>
#include <QRect>
#include <QColor>
enum Quality {
Low = 0, Medium = 1, Good = 2
};
class barSet : public QWidget
{
Q_OBJECT
public:
explicit barSet(QWidget *parent = nullptr);
virtual void paintEvent(QPaintEvent *event);
public slots:
void drawQuality();
private:
Quality _quality;
};
barSet::barSet(QWidget *parent) :
QWidget(parent),
_quality(Quality::Low)
{
update();
}
void barSet::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPainterPath firstPath, secondPath, thirdPath, fourthPath;
firstPath.addRect(QRectF(20, 100, 25, 50));
QPen pen(Qt::white, 3);
painter.setPen(pen);
painter.drawPath(firstPath);
secondPath.addRect(QRectF(50, 75, 25, 75));
painter.drawPath(secondPath);
thirdPath.addRect(QRectF(80, 50, 25, 100));
painter.drawPath(thirdPath);
switch (_quality) {
case Quality::Low:
painter.fillPath(firstPath, QColor(202, 0, 42));
break;
case Quality::Medium:
painter.fillPath(firstPath, Qt::yellow);
painter.fillPath(secondPath, Qt::yellow);
break;
case Quality::Good:
painter.fillPath(firstPath, Qt::green);
painter.fillPath(secondPath, Qt::green);
painter.fillPath(thirdPath, Qt::green);
break;
}
}
void barSet::drawQuality() {
if (_quality != Quality::Excellent) {
_quality = static_cast<Quality>(_quality + 1);
} else {
_quality = Quality::Low;
}
update();
}
Here's the image output of my application:
question from:
https://stackoverflow.com/questions/66067362/keeping-rectangle-border-when-using-qpainterfillpath-in-qt5 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…