//[... somewhere in the widget's header ...]
//[... somewhere in the widget's initialization process ...]
_foreGround
= new QPen();
_foreGround->setCosmetic(true);
_foreGround->setWidth(3);
_backGround
->setColor
(QColor(qRgba
(0,
0,
0,
0)));
//[...]
{
// recalculate the value according to the conditions of QPainter::drawPie
// step 1: subtract _minimum from _value (so the calculation base is always 0)
// step 2: calculate the percentage of value (value/max * 100)
// step 3: translate it to circular conditions (value/max * [100 * 3.6 * 16]) [...] = _FULLCIRCLE
// step 4: take it times -1 (QPainter::drawPie works counter-clockwise with positive values)
int _painterValue = ((_value - _minimum) / double(_maximum - _minimum) * _FULLCIRCLE) * -1;
p.begin(this); // Begin drawing on the widget
// make sure that the widget appears always as a circle, not as an ellipse
int _squaredSize = qMin(this->width(), this->height());
p.setViewport((this->width() - _squaredSize) / 2,(this->height() - _squaredSize) / 2,
_squaredSize, _squaredSize);
// subtract the width of the pen ("the border width") from the widget's rect() to get the drawable area
int _borderWidth = _foreGround->width();
QRect drawableArea
(0 + _borderWidth,
0 + _borderWidth,
this->width() - _borderWidth * 2, this->height() - _borderWidth * 2);
p.
setRenderHint(QPainter::HighQualityAntialiasing,
true);
p.setPen(*_foreGround);
//p.setBrush(*_backGround); <--- DOESN'T WORK
// draw surrounding circle
// _backGround->setStyle(Qt::NoBrush); <--- DOESN'T WORK
p.
setBrush(QBrush(QColor(qRgba
(0,
0,
0,
0)),Qt
::NoBrush));
// <--- WORKS! p.drawEllipse(drawableArea);
// draw pie if value < 0
if (_painterValue < 0)
{
//_backGround->setStyle(Qt::SolidPattern); <--- DOESN'T WORK
p.
setBrush(QBrush(QColor(qRgba
(0,
0,
0,
0)),Qt
::SolidPattern));
// <--- WORKS! p.drawPie(drawableArea, _OFFSET, _painterValue);
// _OFFSET = 1/4 of _FULLCIRCLE (transition from 3 o'clock (drawPie's base) to 12 o'clock)
}
p.end(); // Drawing finished
//[... somewhere in the widget's header ...]
QPen *_foreGround;
QBrush *_backGround;
//[... somewhere in the widget's initialization process ...]
_foreGround = new QPen();
_foreGround->setCosmetic(true);
_foreGround->setWidth(3);
_backGround = new QBrush();
_backGround->setColor(QColor(qRgba(0,0,0,0)));
//[...]
void QtProgressPie::paintEvent(QPaintEvent *e)
{
// recalculate the value according to the conditions of QPainter::drawPie
// step 1: subtract _minimum from _value (so the calculation base is always 0)
// step 2: calculate the percentage of value (value/max * 100)
// step 3: translate it to circular conditions (value/max * [100 * 3.6 * 16]) [...] = _FULLCIRCLE
// step 4: take it times -1 (QPainter::drawPie works counter-clockwise with positive values)
int _painterValue = ((_value - _minimum) / double(_maximum - _minimum) * _FULLCIRCLE) * -1;
QPainter p;
p.begin(this); // Begin drawing on the widget
// make sure that the widget appears always as a circle, not as an ellipse
int _squaredSize = qMin(this->width(), this->height());
p.setViewport((this->width() - _squaredSize) / 2,(this->height() - _squaredSize) / 2,
_squaredSize, _squaredSize);
// subtract the width of the pen ("the border width") from the widget's rect() to get the drawable area
int _borderWidth = _foreGround->width();
QRect drawableArea(0 + _borderWidth, 0 + _borderWidth,
this->width() - _borderWidth * 2, this->height() - _borderWidth * 2);
p.setRenderHint(QPainter::HighQualityAntialiasing, true);
p.setPen(*_foreGround);
//p.setBrush(*_backGround); <--- DOESN'T WORK
// draw surrounding circle
// _backGround->setStyle(Qt::NoBrush); <--- DOESN'T WORK
p.setBrush(QBrush(QColor(qRgba(0,0,0,0)),Qt::NoBrush)); // <--- WORKS!
p.drawEllipse(drawableArea);
// draw pie if value < 0
if (_painterValue < 0)
{
//_backGround->setStyle(Qt::SolidPattern); <--- DOESN'T WORK
p.setBrush(QBrush(QColor(qRgba(0,0,0,0)),Qt::SolidPattern)); // <--- WORKS!
p.drawPie(drawableArea, _OFFSET, _painterValue);
// _OFFSET = 1/4 of _FULLCIRCLE (transition from 3 o'clock (drawPie's base) to 12 o'clock)
}
p.end(); // Drawing finished
To copy to clipboard, switch view to plain text mode
Bookmarks