Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    3
    3674
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      neveralull last edited by

      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);
      readMenu->addAction(readAllAction);
      readMenu->addAction(readOneAction);
      ui->ReadRegisterButton->setMenu(readMenu);
      connect(readAllAction, SIGNAL(triggered()), this, SLOT(readAllRegistersButtonPressed()));
      connect(readOneAction, SIGNAL(triggered()), this, SLOT(readOneRegisterButtonPressed()));
      }
      void RegisterBrowser::readRegisterButtonPressed()
      {
      ui->ReadRegisterButton->showMenu();
      }@
      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()));
      readMenu->exec(globalPos);@

      1 Reply Last reply Reply Quote 0
      • T
        tchoninho last edited by

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

        [code]
        void RegisterBrowser::readRegisterButtonPressed()
        {
        ui->ReadRegisterButton->showMenu();
        }
        [/code]

        Computer Scientist
        Belo Horizonte - Brasil
        C++ Development
        Qt Development

        1 Reply Last reply Reply Quote 0
        • N
          neveralull last edited by

          Thanks! It worked perfectly.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post