PDA

View Full Version : Related QLinearGradient



ashishsaryar
23rd May 2008, 14:25
Hi to all

I m painting a rectangle in QGraphicsItem by QLinearGradient ..
But it is not working properly but same code i run on QWidget paint event then it
is perfect ,..

Pls tell me how can i use QLinearGradient in QGraphicsItem .



QPainter painter(this);
QLinearGradient linearGradient(0, 0, 100, 100);
linearGradient.setColorAt(0.0, Qt::white);
linearGradient.setColorAt(1.0, QColor(253,85,85));
//linearGradient.setColorAt(1.0, Qt::black);
painter.setBrush(linearGradient);
painter.drawRect( 50,50,100,100);

Pls tell me

jpn
25th May 2008, 08:35
How about attaching a couple of screenshots to make "is not working" have some meaning...

ashishsaryar
26th May 2008, 08:14
hi,
Actually i need to fill the rectangle with some gradient effects.
It is basically a QgraphicsItem which i have added to QgraphicsScene.
I have seen some examples and demos which has shown how to paint with gradeint effects.
But thing is that none of the example is related to QgraphicsItem.

I am trying to have gradient affects in a way shown in first post.But the it fills all the rectangle with second color i.e QColor(253,85,85).

If u know the solution please guide where i m wrong.

regards,
ashish

jpn
26th May 2008, 08:36
And it's totally irrelevant to show us how you attempt to do it, right? :p



void paint(...)
{
const QRectF bounds = boundingRect();
QLinearGradient linearGradient(bounds.topLeft(), bounds.bottomRight());
linearGradient.setColorAt(0.0, Qt::white);
linearGradient.setColorAt(1.0, QColor(253,85,85));
//linearGradient.setColorAt(1.0, Qt::black);
painter->setBrush(linearGradient);
painter->drawRect(bounds);
}

ashishsaryar
26th May 2008, 09:29
Thanks ..

Now it is working ..

Pls tell me what i have done wrong earlier ....

I have also given the point (0,0) and bounding rect width and height ..

Pls tell me how it is working ..

Really thanks a lot .

jpn
26th May 2008, 10:09
Pls tell me what i have done wrong earlier ....
How could we? You didn't even show the code for a graphics item, just for a widget... :)