QMenu Context Submenu Problem Qt 5.0.1 Mac



  • When porting my Qt 4.8 application to Qt 5 actions in submenus on context menus are not working properly on the Mac version of Qt 5.0.1. Whenever an item in a submenu is selected the submenu disappears but the main context menu does not exit and the exec() doesn't return. Actions on the main menu work as expected. It works on Windows Qt 5. Am I missing something or is this a bug? Here is a simple test case:

    mainwindow.h:
    @
    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void contextMenu(QPoint p);
    };
    @

    mainwindow.cpp:
    @
    #include "mainwindow.h"

    #include <QDebug>
    #include <QAction>
    #include <QDebug>
    #include <QMenu>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint)));
    }

    MainWindow::~MainWindow()
    {
    }

    void MainWindow::contextMenu(QPoint p) {
    QMenu menu;
    menu.addMenu("Test")->addAction("This Is A Test");

    QAction *a = menu.exec&#40;pos(&#41;+p&#41;;
    
    if(a) {
        qDebug() << "Action:" << a->text();
    } else {
        qDebug() << "No action selected";
    }
    

    }
    @



  • This seems to work as a work around for now:

    @
    void MainWindow::contextMenu(QPoint p) {
    QMenu menu;
    QMenu *testMenu = menu.addMenu("Test");
    QAction *testAction = testMenu->addAction("This Is A Test");

    connect(testAction, SIGNAL(triggered()), &menu, SLOT(close()));
    
    QAction *a = menu.exec&#40;pos(&#41;+p);
    
    if(a) {
        qDebug() << "Action:" << a->text();
    } else {
        qDebug() << "No action selected";
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.