multiple colors in a single rectitem
hi,some one please help me .Is it possible to put two are more colors in a single rectItem .for an example QRect(0,0,100,100) this is my actual rectitem i want to put half area red and remaing half green.please help with some sample code
April 28, 2012
Sam
Area 51 Engineer
609 posts
Hi ,
you can try the following code :
QRect myRect
(0,0,100,100);
painter.
fillRect(myRect.
x(),myRect.
y(),myRect.
width()/2,myRect.
height(),Qt::red);
painter.
fillRect(myRect.
width()/2,myRect.
y(),myRect.
width(),myRect.
height(),Qt::green);
and if you are overriding the paint() event of a component/widget then u can write:
painter
->fillRect(QRect(option.
rect.
x(),option.
rect.
y(),option.
rect.
width()/2,option.
rect.
height()),Qt::red);
painter
->fillRect(QRect(option.
rect.
width()/2,option.
rect.
y(),option.
rect.
width(),option.
rect.
height()),Qt::green);
April 29, 2012
Sam
Area 51 Engineer
609 posts
Hi,
If you need to subclass QGraphicsItem or QGraphicsRectItem as per your requirements, and then override the paint() function.
For Eg:
.h
{
public:
MyRectItem();
};
.cpp
QRectF MyRectItem
::boundingRect() const
{
}
{
painter
->fillRect(boundingRect
().
x(),boundingRect
().
y(),boundingRect
().
width()/2,boundingRect
().
height(),Qt::red);
painter
->fillRect(boundingRect
().
width()/2,boundingRect
().
y(),boundingRect
().
width(),boundingRect
().
height(),Qt::green);
}