wrongusername
wrongusername

Reputation: 18918

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?

Upvotes: 2

Views: 2387

Answers (2)

victor
victor

Reputation: 11

Change:

radial.setColorAt( 0, Qt::black );

To:

radial.setColorAt( n, Qt::black );

n being a number between 0 and 1.

Upvotes: 1

Pie_Jesu
Pie_Jesu

Reputation: 1914

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);

Upvotes: 4

Related Questions