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. How to read the color of an rectitem
Forum Updated to NodeBB v4.3 + New Features

How to read the color of an rectitem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.9k 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 25 Nov 2011, 07:41 last edited by
    #1

    hi,in my app i have a graphicsscene and lot of rectitems with different colours...i want to read the color of an perticular rectitem ... how to read the color of an rectitem ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 25 Nov 2011, 08:51 last edited by
      #2

      QGraphicsRectItem is derived from QGraphicsShapeItem, which as properties called pen and brush. The first is used for the outline of the shape, the second for the fill. Both contain a color.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mahesh
        wrote on 25 Nov 2011, 09:23 last edited by
        #3

        Thanks for the reply..i already done setBresh to the rectitem now my doubt is

        @
        QGraphicsRectItem *rectItem=new QGraphicsRectItem(20,30,40,50);
        rectItem->setBrush( QBrush(Qt::green));
        @
        i added color like this and then i added this rectitem to the scene...

        @void View ::mousePressEvent(QMouseEvent* event) {

        if (event->button() == Qt::LeftButton){

        if (QGraphicsRectItem *item = itemAt(event->pos())) {

        //here i have the rectitem i want to find the color of this item
        }

        }

        }
        @

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rokemoon
          wrote on 25 Nov 2011, 09:48 last edited by
          #4

          First of all "itemAt":http://doc.qt.nokia.com/latest/qgraphicsview.html#itemAt return QGraphicsItem , so you shoud cast it to your QGraphicsRectItem and check pointer on null.
          @
          void View ::mousePressEvent(QMouseEvent
          event) {

          if (event->button() == Qt::LeftButton){
          QGraphicsRectItem *item = static_cast<QGraphicsRectItem *>(itemAt(event->pos()));
          if (item) {
          QBrush brush = item->brush(); // and so on
          //here i have the rectitem i want to find the color of this item
          }
          }
          }
          @
          Try it, I don't test it.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mahesh
            wrote on 25 Nov 2011, 12:08 last edited by
            #5

            it is showing rgb format ...how can it convet it into name format(ex:blue,green)...

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 25 Nov 2011, 12:34 last edited by
              #6

              [quote author="phani438" date="1322222885"]it is showing rgb format ...how can it convet it into name format(ex:blue,green)...
              [/quote]

              Other than building your own table of rgb->name mappings, you can not.

              1 Reply Last reply
              0

              1/6

              25 Nov 2011, 07:41

              • Login

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