PDA

View Full Version : QLinearGradient question



^NyAw^
7th March 2008, 16:19
Hi,

I want to paint a Widget with a QLinearGradient:



QLinearGradient qGradient;

QGradientStops qStops;
QGradientStop qStop1(0.0,QColor(255,0,0));
QGradientStop qStop2(0.5,QColor(255,255,255));
QGradientStop qStop3(1.0,QColor(0,255,0));

qStops.append(qStop1);
qStops.append(qStop2);
qStops.append(qStop3);
qGradient.setStops(qStops);

QPainter painter(this);
painter.fillRect(rect(),qGradient);



What I want is a gradient from Red to White to Green but I only get a Green Widget.

Thanks,

wysota
7th March 2008, 16:47
You have to define the size and direction of the gradient.


QLinearGradient grad(0,0,width(), height());
grad.setColorAt(0, Qt::red);
grad.setColorAt(0.5, Qt::white);
grad.setColorAt(1, Qt::green);

QPainter p(this);
p.fillRect(rect(), grad);

peppino
7th March 2008, 16:51
try with



QLinearGradient linGrad(0, 0, 40, 40);
linGrad.setColorAt(0, Qt::red);
linGrad.setColorAt(0.5, Qt::white);
linGrad.setColorAt(1, Qt::green);
linGrad.setSpread(QGradient::PadSpread);
painter.setBrush(linGrad);
painter.drawRect(0, 0, 40, 40);


n.b: here the x/y coords of rect is casual ....

cheers

^NyAw^
7th March 2008, 16:52
Hi,

Thanks,:)