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

  • Just draw a red rectangle and a green rectangle next to it.

  • Hi ,
    you can try the following code :
    QRect myRect(0,0,100,100);
    QPainter painter(this);

    and if you are overriding the paint() event of a component/widget then u can write:


  • Thanks to all. "SOUMITRA" your code is very useful for me.actually i am working on can i do the same "color " thing on the QGRaphicsRectItem instead of QRect

  • Hi,

    If you need to subclass QGraphicsItem or QGraphicsRectItem as per your requirements, and then override the paint() function.

    For Eg:
    @class MyRectItem : public QGraphicsItem


    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    @QRectF MyRectItem::boundingRect() const
    return QRectF(0,0,100,100);

    void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)


  • Or just use 2 QGraphicsRectItems with a common parent.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.