Search code examples
qtopenglqglwidget

How to show a popup menu using QGLWidget?


how to show a context menu when you right click within a QGLWidget?


Solution

  • Override the QGLWidget class and the mouseReleaseEvent( QMouseEvent * event ) function
    Then in the mouseReleaseEvent function, call QMenu exec() with the mapped global position.

    void MyWidget::mouseReleaseEvent ( QMouseEvent * event )
    {
        if(event->button() == Qt::RightButton)
        {
            QMenu menu;
    
            QAction* openAct = new QAction("Open...", this);
    
            menu.addAction(openAct);
    
            menu.addSeparator();
            menu.exec(mapToGlobal(event->pos()));
        }
        QGLWidget::mouseReleaseEvent(event);  //Dont forget to pass on the event to parent
    }