Question

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

Was it helpful?

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top