Position of an object mapped relative to its parent



  • I have a widget with several objects in it.
    The position of the objects inside is calculated based on its relative position to its parent.
    I use that position to execute my mouse right click.

    Unfortunately when I scroll up and down inside the main widget itself, and right click on an object inside, the menu does not appear where I click, but somewhere else, based perhaps on where the object would have been earlier had I not moved the main widget..

    Please suggest a workaround. I'm new at this.

    @QGraphicsScene *parentScene = this->scene();
    //Get parent views
    QList<QGraphicsView *> parentViewList = parentScene->views();
    qDebug()<<parentViewList.at(0);
    QPoint viewPos = parentViewList.at(0)->mapToGlobal(QPoint(event->pos().x(),event->pos().y()));

    //int x = this->pos().x() + viewPos.x();
    //int y = this->pos().y() + viewPos.y();

    menu.exec(QPoint((this->pos().x() + viewPos.x()),(this->pos().y() + viewPos.y())));
    }
    @

    Thank you.



  • Bump..

    Anyone, please?
    I'm new to this, so would like some help.
    Thank you.


  • Moderators

    Have you tried using maptFromGlobal() instead?



  • Solved it.
    Simple matter of using

    exec(QCursor::pos());


Log in to reply
 

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