Solved Popup Menu in QOpenGLWindow - how to do it?
-
I have a program that processes data and then generates an image which is then displayed in a QOpenGLWidow. I would like to have a popup menu on right mouse click to give a list of display options. The menu changes according to the properties of the data/image.
As far as I can see, QOpenGLWindow (and QWindow) do not have a contextMenuPolicy, so trying to detect a contextMenuEvent on a right click (as below) fails and nothing happens
void ViewData::contextMenuEvent(QContextMenuEvent *e) { QMenu* menu =imageMenu; menu->exec(e->globalPos()); }
I wondered if creating a QContextMenuEvent on a right mouse click might help, but I'm not sure how to transmit it and by itself it does nothing.
-
@DavidRS You could simply show the pop-up menu on right-click without transmitting QContextMenuEvent
-
Nope. If you do the following
void ViewData::mousePressEvent(QMouseEvent *e) { switch(e->button()) { // code for other buttons before here case (Qt::RightButton) { imageMenu->exec(e->globalPos()); break; } }
The program crashes when you right click.
This is running under Linux and X11 with Qt ver 5.13.2 -
Hi,
Might be a silly question but are you sure that imageMenu is valid ?
Can you show the stack trace you get ?
-
@SGaist You were right - the QMenu initialization was in a branch that was not being visited. Once I moved it before that branch it worked perfectly. Because there are no widgets the menu was created as
imageMenu = new QMenu(nullptr);
and was directly accessed through the right mouse click once the image was created.