Qmenu popup
-
@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
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
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. -
@Oumayma
I would guess the (bottom left) position of the item the mouse clicks on on the menu bar? -
@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
Edicionetc.) 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
I have added this line of code:
ui->menuArchivo->exec(QMenu(ui->menuArchivo).rect().bottomLeft());
this is the result:

@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 thebottomLeftinmenuArchivocoordinates (probably something like 0/20). Butexecuses global/your MainWindow's coordinates. This is why your popup starts at (0/20) but from the top of yourMainWindow.
So try to map yourbottomLeftto parent or global (withui->menuArchivo->mapToGlobal(ui->menuArchivo->rect().bottomLeft())) <- not tested. Play around until you get a satisfying result.

