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 make my custom QGraphicsPolygonItem() selectable ?
QtWS25 Last Chance

How to make my custom QGraphicsPolygonItem() selectable ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.0k Views
  • 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.
  • K Offline
    K Offline
    keresan
    wrote on last edited by
    #1

    hi,
    i make my custom simpleItem : QGraphicsPolygonItem()

    in constructor i set this->setFlag(QGraphicsItem::ItemIsSelectable); so item is selectable. everything is good - when i click on item, the black-white dashline appereas around it.
    but when i reimplemented paint(), selectivity has been broken. so i reimplemented boundingRect() and shape(), but not helped.

    Which another methods i have to reimplemeted to make my simpleItem selectable ?

    thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      yes, all the magic are in your paint function. what the item looks like depends on what you paint in the function.

      And seems like you want get different appearance when its status(hover/selected/pressed...) changed, so you should draw different thing depends on its status.

      For example:

      @
      ...
      if (isSelected())
      ...;
      else
      ....;
      ...
      @

      Regards,

      Debao

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        In your own paint function, did you call the paint function of the base class, in your case QGraphicsPolygonItem? If you want to keep functionality of the parent class (and you generally do!) make sure you call the base class implementation of any virtual method you reimplement in your reimplementation.

        So:
        @
        void MyGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
        {
        QGraphicsPolygonItem::paint(painter, option, widget); //<-- Let the base class do its job!
        //your own painting operations
        }
        @

        Obviously, you might want to change the options you pass, or do (some of) your own painting first, but in general you will want to use this pattern for any virtual function your reimplement.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          keresan
          wrote on last edited by
          #4

          this is exactly what i want, thanks

          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