[SOLVED]mousePress event QGraphicsView



  • Hello,

    I have a subclassed QGraphicsView and added mousePressEvent. When clicked on the view, it creates a QGraphicsItem at the same location (via QGraphicsScene). This QGraphicsItem has also a mousePressEvent.

    I want that when you click on the item created, it doesnt activate the event on the scene. (otherwise it creates a second item at the click location).

    How could I achieve that?

    Cheers,



  • Still no idea? Raven is not around? :)

    Cheers,


  • Moderators

    patience is the key ;)
    your thread just has 25 views so far.

    Did i understand you correct that you re-implemented the mousePressEvent() handler of the QGraphicsView subclass? Meaning QGraphicsView::mousePressEvent()?


  • Moderators

    i assume you did... so try the following:

    QGraphicsScene is responsible for forwarding the events to the items.
    So you should subclass the scene and re-implement the mousePressEvent() like this:
    @
    void MyGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    {
    QGraphicsScene::mousePressEvent ( mouseEvent );
    if( mouseEvent->isAccepted() )
    return;

     //if the event has not been processed by an item create a new item here
    

    }
    @

    This only works if you have accepted the mouseEvent in your item!



  • Hello raven :)

    Thanks for your answer !

    [quote author="raven-worx" date="1381411476"]patience is the key ;) your thread just has 25 views so far. Did i understand you correct that you re-implemented the mousePressEvent() handler of the QGraphicsView subclass? Meaning QGraphicsView::mousePressEvent()?[/quote]

    yes I subclassed like that.

    I will try to subclass the scene as you mentionned it. My question is : is that enough to subclass the scene by adding mousePressEvent without subclassing the view or do I need both? (subclass scene and view)


  • Moderators

    only the scene.



  • Dear raven, I tried :

    @
    void XCustomGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * event )
    {
    QGraphicsScene::mousePressEvent ( event );
    if( event->isAccepted() )
    {
    return;
    }
    else{
    emit signalCreateRectangle(event->pos());
    qDebug() << event->pos();
    qDebug() << this->width();
    qDebug() << this->height();
    update();
    QGraphicsScene::mousePressEvent(event);
    }
    //if the event has not been processed by an item create a new item here
    }
    @

    and the qDebug() << event->pos(); only displays QPointF(0,0), wherever I click ...


  • Moderators

    use event->scenePos()

    and also don't call the base class implementation a second time.



  • Dear raven,

    it works if I add :

    @QGraphicsScene::mousePressEvent ( event );@

    at the end and not at the beginning. However, when moving the item around, it creates a second rectangle when I click to select it ...
    It only works if I dont move the item ...

    Any idea? :)


  • Moderators

    [quote author="BlackMamba" date="1381426485"]
    it works if I add :

    @QGraphicsScene::mousePressEvent ( event );@

    at the end and not at the beginning.
    [/quote]
    This doesnt make sense.

    [quote author="BlackMamba" date="1381426485"]
    However, when moving the item around, it creates a second rectangle when I click to select it ...
    [/quote]
    I guess that is...when you create a rect item every time you receive a mouse press event?



  • It works ! Just needed to add update somewhere else that it draws properly.
    Indeed, QGraphicsScene::mousePressEvent ( event ); should be at the beginning.

    Thank you so much raven you help me a lot :)


Log in to reply
 

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