// Can check the menu items
actionA->setCheckable(true);
actionB->setCheckable(true);
actionC->setCheckable(true);
// only one at a time
group->setExclusive(true);
group->addAction(actionA);
group->addAction(actionB);
group->addAction(actionC);
// build a menu of them
menu->addAction(actionA);
menu->addAction(actionB);
menu->addAction(actionC);
// and a tool button to put the menu on
button->setDefaultAction(actionA);
button->setMenu(menu);
connect(menu,
SIGNAL(triggered
(QAction*)), button,
SLOT(setDefaultAction
(QAction*)));
// << magic sauce // put it on the tool bar
toolBar->addWidget(button);
addToolBar(Qt::TopToolBarArea, toolBar);
// Can check the menu items
QAction *actionA = new QAction("AAA", this);
actionA->setCheckable(true);
QAction *actionB = new QAction("BBB", this);
actionB->setCheckable(true);
QAction *actionC = new QAction("CCC", this);
actionC->setCheckable(true);
// only one at a time
QActionGroup *group = new QActionGroup(this);
group->setExclusive(true);
group->addAction(actionA);
group->addAction(actionB);
group->addAction(actionC);
// build a menu of them
QMenu *menu = new QMenu(this);
menu->addAction(actionA);
menu->addAction(actionB);
menu->addAction(actionC);
// and a tool button to put the menu on
QToolButton *button = new QToolButton(this);
button->setDefaultAction(actionA);
button->setMenu(menu);
connect(menu, SIGNAL(triggered(QAction*)), button, SLOT(setDefaultAction(QAction*))); // << magic sauce
// put it on the tool bar
QToolBar *toolBar = new QToolBar(this);
toolBar->addWidget(button);
addToolBar(Qt::TopToolBarArea, toolBar);
To copy to clipboard, switch view to plain text mode
Bookmarks