Important: Please read the Qt Code of Conduct -

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;

    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.

  • Lifetime Qt Champion

    @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)
        // code for other buttons before here
          case (Qt::RightButton)

    The program crashes when you right click.
    This is running under Linux and X11 with Qt ver 5.13.2

  • Lifetime Qt Champion


    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.

Log in to reply