Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Context menu position problem
Forum Update on Monday, May 27th 2025

Context menu position problem

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!!

    JonBJ 1 Reply Last reply
    0
    • ivanicyI ivanicy

      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!!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      ivanicyI 1 Reply Last reply
      0
      • JonBJ JonB

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

        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • ivanicyI ivanicy

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          ivanicyI 1 Reply Last reply
          0
          • JonBJ JonB

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

            ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by
            #5

            @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!!!

            JonBJ 1 Reply Last reply
            0
            • ivanicyI ivanicy

              @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!!!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              ivanicyI 1 Reply Last reply
              2
              • JonBJ JonB

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

                ivanicyI Offline
                ivanicyI Offline
                ivanicy
                wrote on last edited by ivanicy
                #7

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

                JonBJ 1 Reply Last reply
                0
                • ivanicyI ivanicy

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

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

                  ivanicyI 1 Reply Last reply
                  2
                  • JonBJ JonB

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

                    ivanicyI Offline
                    ivanicyI Offline
                    ivanicy
                    wrote on last edited by
                    #9

                    @JonB This was the solution!!

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

                    works!!

                    Thank you very much!

                    JonBJ 1 Reply Last reply
                    0
                    • ivanicyI ivanicy

                      @JonB This was the solution!!

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

                      works!!

                      Thank you very much!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @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!

                      ivanicyI 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @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!

                        ivanicyI Offline
                        ivanicyI Offline
                        ivanicy
                        wrote on last edited by
                        #11

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

                        JonBJ 1 Reply Last reply
                        1
                        • ivanicyI ivanicy

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

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved