Unsolved Qmenu popup
-
¿What is the method that displays the menu in the position that appears in the image when you click on it?
-
@Oumayma question not clearly understood.
https://doc.qt.io/qt-5/qmenu.html
is it about QMenu::addAction ? or QAction *QMenu::exec()?
-
@nagesh I explain the question: when you click on a menu, its actions are displayed as shown in the image, I would like to know what method makes that happen.
-
@Oumayma
QMenu::exec()
. -
@JonB when using this method it displays the menu in another position.
-
@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());
-
@JonB I have already tried the options that it indicates to me, what I want is to know from where to get the position in which the menu is displayed as it does in the first image.
thanks. -
This post is deleted! -
@Oumayma
I would guess the (bottom left) position of the item the mouse clicks on on the menu bar? -
@JonB ¿There is any method that returns me the lower left position of a QMenu?
-
@Oumayma
I have added this line of code:
ui->menuArchivo->exec(QMenu(ui->menuArchivo).rect().bottomLeft());
this is the result:
-
@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.