qtdouble-clickqgraphicsitemqgraphicstextitemqgraphicspixmapitem

QGraphicPixmapItem double click event does not go to parent widget while QGraphicsTextItem send


I have a main widget in Qt and this widget contains a QGraphicsView and inside it QGraphicsScene. In scene I add QGraphicsPixmapItems and QGraphicsTextItems. In main widget I handle QWidget::mouseDoubleClickEvent ( QMouseEvent * event ), my items are all set flags with:

mItem->setFlag ( QGraphicsItem::ItemIsMovable );
mItem->setFlag ( QGraphicsItem::ItemIsSelectable );
mItem->setFlag ( QGraphicsItem::ItemIsFocusable);

Because I want to move items in scene and select them and also I want when double click occurs main widget handles that. When I double click onto QGraphicsTextItem it enters to mouseDoubleClickEvent in main widget, however when I double click to QGraphicsPixmap item, it absorbs double click and does not send it to main widget. Also when ItemIsFocusable flag is not set, QGraphicsTextItem also does absorb the double click event. Why does it occur?.
I did not want to implement subclass of QGraphicsItems and wanted to use already defined methods. Here is a picture of what I do:

enter image description here


Solution

  • I found a solution for this because my QGraphicsPixmapItem and QGraphicsTextItem behaves differently on double clicking: QGraphicsTextItem sends its double click event to parent while QGraphicsPixmapItem does not, I commented out the ItemIsFocusable property :

    mItem->setFlag ( QGraphicsItem::ItemIsMovable );
    mItem->setFlag ( QGraphicsItem::ItemIsSelectable );
    //mItem->setFlag ( QGraphicsItem::ItemIsFocusable);
    

    Because even if ItemIsFocusable is a QGraphicsItem property, it does not behave same in different QGraphicsItem inherited classes, so for handling double click I installed an event filter on QGraphicsScene that contains QGraphicsItems in main widget.

    this->ui.graphicsViewMainScreen->scene ( )->installEventFilter ( this );
    

    And as implementation of event filter :

    bool MyMainWidget::eventFilter ( QObject *target , QEvent *event )
    {
        if ( target == this->ui.graphicsViewMainScreen->scene ( ) )
        {
            if ( event->type ( ) == QEvent::GraphicsSceneMouseDoubleClick )
            {
                QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent *>( event );
            }
        }
        return false;
    }
    

    Now I can detect double clicks on QGraphicsItems on my main widget's scene.