Search code examples
c++qtqtwidgetsqgradient

How to create a QRadialGradient


I am trying to create a circle with a gradient fill:

//I want the center to be at 10, 10 in the circle and the radius to be 50 pixels
QRadialGradient radial(QPointF(10, 10), 50);
radial.setColorAt(0, Qt::black); //I want the center to be black
radial.setColorAt(1, Qt::white); //I want the sides to be white
painter.setBrush(QBrush(radial));
painter.drawEllipse(/*stuff*/);

However, all this accomplishes is to show me a totally white circle. How can I rectify this?


Solution

  • It will be white because you are using the wrong coordinates.

    If you set the gradient for your widget (in your case it's only a small area), you can paint your ellipse in the wrong place, and it will be surely white:

    Set the gradient's coordinates correctly. e.g:

    QRadialGradient radial(QPointF(100, 100), 50);
    // ...
    painter.drawEllipse(50,50,100,100);