Qmenu popup
-
@Oumayma
Read https://doc.qt.io/qt-5/qmenu.html#execThis is equivalent to exec(pos()).
In most situations you'll want to specify the position yourself, for example, the current mouse position:
exec(QCursor::pos());
or aligned to a widget:
exec(somewidget.mapToGlobal(QPoint(0,0)));
or in reaction to a QMouseEvent *e:
exec(e->globalPos());
-
@Oumayma
So you need to move it down a bit, for whatever reason?Before you spend too long on this. I seem to remember quite a while ago that someone asked to be able to access the individual menu actions like this for their own purpose. And I have a feeling that he never got an answer as to how to access the correct horizontal position for each of the items in the menu (your
Edicion
etc.) Even if you can the vertical position correct, you had better make sure you can get the horizontal position of each item after the first, left-most, else you won't get anywhere....I may be wrong. After all, the Qt code must be able to get that right (maybe you should look at its source code), so perhaps I misremember and you can get the correct horizontal position, but you should check.
-
@Oumayma said in Qmenu popup:
@Oumayma
I have added this line of code:
ui->menuArchivo->exec(QMenu(ui->menuArchivo).rect().bottomLeft());
this is the result:
Map the point to correct coordinate system.
The rect returns thebottomLeft
inmenuArchivo
coordinates (probably something like 0/20). Butexec
uses global/your MainWindow's coordinates. This is why your popup starts at (0/20) but from the top of yourMainWindow
.
So try to map yourbottomLeft
to parent or global (withui->menuArchivo->mapToGlobal(ui->menuArchivo->rect().bottomLeft())
) <- not tested. Play around until you get a satisfying result.