Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple colors in a single rectitem
Forum Updated to NodeBB v4.3 + New Features

Multiple colors in a single rectitem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahesh
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

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

      http://anavi.org/

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        Hi ,
        you can try the following code :
        @
        QRect myRect(0,0,100,100);
        QPainter painter(this);
        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);@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mahesh
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Hi,

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

            For Eg:
            .h
            @class MyRectItem : public QGraphicsItem
            {

            public:
            MyRectItem();

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

            };@
            .cpp
            @QRectF MyRectItem::boundingRect() const
            {
            return QRectF(0,0,100,100);
            }

            void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
            {
            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);

            }@

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Or just use 2 QGraphicsRectItems with a common parent.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved