Important: Please read the Qt Code of Conduct -

[SOLVED] QPushbutton with QMenu - menu does not go away until selected twice

  • Is this a bug, or am I doing something wrong? When I put a QMenu in a QPushButton, and the button is pushed, a menu appears. If I select a menu item, the action is triggered, but the menu stays displayed. If I select the same action again, or a different action, then the menu disappears. I want the menu to disappear every time I select an action.
    @void RegisterBrowser::initializeMenus()
    { readMenu = new QMenu("Read Menu", mainWindow);
    readAllAction = new QAction("Read All", mainWindow);
    readOneAction = new QAction("Read One", mainWindow);
    connect(readAllAction, SIGNAL(triggered()), this, SLOT(readAllRegistersButtonPressed()));
    connect(readOneAction, SIGNAL(triggered()), this, SLOT(readOneRegisterButtonPressed()));
    void RegisterBrowser::readRegisterButtonPressed()
    It works perfectly if I don't put the QMenu in the QPushButton, and handle it manually like this:
    @ QPoint globalPos = ui->ReadRegisterButton->mapToGlobal(QPoint(0,ui->ReadRegisterButton->height()));

  • Remove the code below from 'readRegisterButtonPressed()'.
    Qt will show the menu automatically..

    void RegisterBrowser::readRegisterButtonPressed()

  • Thanks! It worked perfectly.

Log in to reply