Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to make my custom QGraphicsPolygonItem() selectable ?

    General and Desktop
    3
    4
    2722
    Loading More Posts
    • 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
      keresan last edited by

      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 Reply Quote 0
      • D
        dbzhang800 last edited by

        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 Reply Quote 0
        • A
          andre last edited by

          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 Reply Quote 0
          • K
            keresan last edited by

            this is exactly what i want, thanks

            1 Reply Last reply Reply Quote 0
            • First post
              Last post