Best way to make a QToolBar of "checkable" QToolButtons where only one of the buttons can be checked at a time?
Asked Answered
M

1

9

I'm looking to make a QToolBar with a few actions in it, each of which is "checkable" (that is, I call setCheckable(true) on each action after creating it, which leaves the button in the down state after clicking it).

The only way I can think of "unchecking" the other buttons is to hook into each button's triggered signal and uncheck the other buttons when a given button is checked.

Is there a better way?

Mop answered 1/7, 2010 at 1:7 Comment(0)
F
17

Create a QActionGroup and let it be the parent of your actions. This QActionGroup will maintain the states of its children.

QActionGroup *anActionGroup = new QActionGroup(yourParentWidget);
QAction* action1 = new QAction("Action 1", anActionGroup);
QAction* action2 = new QAction("Action 2", anActionGroup);
QAction* actionN = new QAction("Action N", anActionGroup);
action1->setCheckable(true);
action2->setCheckable(true);
actionN->setCheckable(true);

// Add these action to the tool bar
Foreshank answered 1/7, 2010 at 3:29 Comment(1)
That should work, for the general case of n exclusive buttons, there is QButtonGroup, too.Substance

© 2022 - 2024 — McMap. All rights reserved.