How to make my custom QGraphicsPolygonItem() selectable ?

  • 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 ?


  • 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())



  • 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.

    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.

  • this is exactly what i want, thanks

Log in to reply

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