How to show popup menu using QGLWidget?

how to show context menu on right click in QGLWidget?

+2


a source to share


1 answer


Override QGLWidget class and mouseReleaseEvent function (QMouseEvent *)
Then, in mouseReleaseEvent function, call QMenu exec () with the displayed 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
}

      

+1


a source







All Articles