Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Qt 4.7, how can a pop-up menu be added to a QToolbar button?

Tags:

c++

qt

I am newbie to Qt and am currently playing about with Qt Creator and raw C++ code. I have managed to get some simple functionality working, including a menu and toolbar by adding QAction objects to both. However I am having some difficulty:

What I would like to do is have a menu option which has a submenu, e.g. New, with a submenu with a couplwe of items, and duplicate this on the QToolbar. I have managed it in the menu: New as a QMenu, and submenu items as QAction objects. I cannot see how to get this to work in the toolbar, e.g. a New button which, when clicked, would run the "default" QAction (such as the first submenu item), but with a smaller menu button to the right offering all other options. I suppose this is analogous to the Undo/Redo buttons on the Office toolbars.

like image 374
BWHazel Avatar asked Mar 19 '11 21:03

BWHazel


People also ask

How do I add menu bar to QT?

In most main window style applications you would use the menuBar() function provided in QMainWindow, adding QMenus to the menu bar and adding QActions to the pop-up menus. Example (from the Menus example): fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct);

What is QToolBar?

A QToolBar widget is a movable panel consisting of text buttons, buttons with icons or other widgets. It is usually situated in a horizontal bar below menu bar, although it can be floating.

What is Tool button in Qt Designer?

A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead.


2 Answers

I believe QToolButton widget should work fine for what you're trying to do, see if and example below would work for you:

QMenu *menu = new QMenu();
QAction *testAction = new QAction("test menu item", this);
menu->addAction(testAction);

QToolButton* toolButton = new QToolButton();
toolButton->setMenu(menu);
toolButton->setPopupMode(QToolButton::InstantPopup);
toolBar->addWidget(toolButton);

hope this helps, regards

like image 95
serge_gubenko Avatar answered Oct 21 '22 06:10

serge_gubenko


serge_gubenco's answer will work, except when the window needs to be resized smaller and Qt tries to put the QToolButton itself in a popup menu. It may be unable do so. See http://doc.qt.io/archives/qt-4.7/qtoolbar.html.

The solution is to use a quick QWidgetAction, as below in a modified snippet.

QMenu *menu = new QMenu();
QAction *testAction = new QAction("test menu item", this);
menu->addAction(testAction);

QToolButton* toolButton = new QToolButton();
toolButton->setMenu(menu);
toolButton->setPopupMode(QToolButton::InstantPopup);

QWidgetAction* toolButtonAction = new QWidgetAction(this);
toolButtonAction->setDefaultWidget(toolButton);

toolBar->addAction(toolButtonAction);
like image 45
richarddonkin Avatar answered Oct 21 '22 06:10

richarddonkin