Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How does QRadialGradient work??
Diracsbracket last edited by
I am trying to get a QRadialGradient to display a simple gradient from the center of a circle to its boundary.
The Widget in which I display this circle is 400x400px and I use the following code:
QPainter painter(this); QRadialGradient radialGrad(QPointF(300, 300), 200); radialGrad.setColorAt(0, Qt::white); radialGrad.setColorAt(0.5, Qt::red); radialGrad.setColorAt(1, Qt::white); QBrush brush(radialGrad); painter.setBrush(brush); QPen pen; pen.setStyle(Qt::NoPen); painter.setPen(pen); painter.drawEllipse(200, 200, 200, 200);
What I don't understand is why in the name, I need to use
QRadialGradient radialGrad(QPointF(300, 300), 200);
To get the gradient centered as desired at the center of the circle. I first tried with the more logical setting:
QRadialGradient radialGrad(QPointF(200, 200), 200);
But this offsets the gradient such that it is no longer centered:
Were does the 300 value come from that I need to use to center my gradient?
painter.drawEllipse(200, 200, 200, 200);
you draw your circle into a rectangle at (200,200) with the size of 200x200.
This means the center is at (300,300)
So your assumption that the size of widget is 400x400 is wrong for some reason (otherwise you wouldn't see the whole circle).
I personally prefer to use
gradient.setCoordinateMode(QGradient::ObjectBoundingMode). This lets you set the points relative (from 0 to 1) to the object boundaries itself.
Diracsbracket last edited by Diracsbracket
ou draw your circle into a rectangle at (200,200) with the size of 200x200.
Aaaahhh.... my bad.... I just saw the other overloaded function which took a QPoint() for the center and rx and ry for the radii, but I used the one where the 4 arguments define a rectangle... and just assumed that the two first arguments were the center coordinates, and the other the radii... I guess it's time to go sleep...