Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QPushbutton with QMenu - menu does not go away until selected twice
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.0k Views 1 Watching
  • 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 Offline
    N Offline
    neveralull
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tchoninho
      wrote on last edited by
      #2

      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
      0
      • N Offline
        N Offline
        neveralull
        wrote on last edited by
        #3

        Thanks! It worked perfectly.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved