Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Context menu position problem



  • Hello!

    I had installed a contextMenu in a QGroupBox. This is the slot that receives the signal:

    void MainWindow::showGroupsMenu(const QPoint &point) {
        QGroupBox* group = qobject_cast<QGroupBox*>(sender());
        qDebug() << "Group: " << group->title();
    
        QMenu contextMenu(tr("Context menu"), group);
    
        QAction action1("Add camera", this);
    //    connect(&action1, SIGNAL(triggered()), this, SLOT(removeDataPoint()));
        contextMenu.addAction(&action1);
    
        qDebug() << "Pos: " << point;
    
        contextMenu.exec(mapToGlobal(point));
    }
    

    The problem here is the execution position. It always puts the context menu in the topLeft side of the window and my QGroupBox is in the middle of the screen, for example. How can I fix it??

    Thank you very much!!



  • @ivanicy
    Change the mapToGlobal(point) to the desired position if it's not already correct/what you want?



  • @JonB point contains the relative position in the QGroupBox and I want to show the contextMenu in the absolute position



  • @ivanicy
    You don't say whether you are claiming the point received in the function is correct? Are you saying it;s always (0, 0), or what?

    Anyway, if you need to add the QGroupBox coords to it, maybe you need something like group->mapToGlobal(), e.g. does group->mapToGlobal(QPoint(0,0)) at least put it at top left of group box?



  • @JonB Let me try to explain it with a picture.

    63ccd774-a06b-42e7-88ba-a61fb28db898-image.png

    Now, with my code, when I right click hover a QGroupBox, the context menu appears in the topleft corner and there is a displacement in function of the relative coordinates of the QGroupBox (they are between 0 and QGroupBox size). I need to put the context menu in the right click place.

    I hope I explain better this time.

    Thank you very much for your time!!!



  • @ivanicy
    Did you try group->mapToGlobal(QPoint(0,0)) to see if that places it at top left of group box?



  • @JonB This shows the context menu in the topleft corner of the screen, not on the QGroupBox



  • @ivanicy
    May I verify you are trying group->mapToGlobal(QPoint(0, 0)) and not just mapToGlobal(QPoint(0, 0))? Because https://doc.qt.io/qt-5/qwidget.html#mapToGlobal states

    Translates the widget coordinate pos to global screen coordinates. For example, mapToGlobal(QPoint(0,0)) would give the global coordinates of the top-left pixel of the widget.



  • @JonB This was the solution!!

    menuContext->exec(group->mapToGlobal(point));
    

    works!!

    Thank you very much!



  • @ivanicy
    But that's what I said you needed, i.e. group->mapToGlobal(QPoint(0, 0)), to make it relative to group, as per the docs! You should not say

    [Me] Did you try group->mapToGlobal(QPoint(0,0)) to see if that places it at top left of group box?
    [You] @JonB This shows the context menu in the topleft corner of the screen, not on the QGroupBox

    if you did not copy & paste what I suggested, I suspected you were only trying mapToGlobal(QPoint(0, 0)), it makes it very hard to help people if they tell you they tried what was suggested but in fact did not!



  • @JonB Yes, sorry friend, you are right. I didn't read the "group" word the first time. Sorry again



  • @ivanicy
    It's OK, I did not intend to be surly, just to impress how important it is to follow instructions carefully and to say exactly what you did/did not do, for everyone's benefit.


Log in to reply