Mutually Exclusive Checkable Menu and Toolbar Actions
-
I have a pair of menu actions, that also have toolbar buttons, that are checkable as they are used to select one of two modes for the application. They are set up in designer, but work independently, so checking one does not uncheck the other.
I figured it was just a matter of creating a QActionGroup and adding them to it. My MainWindow class has a member that is a pointer to a QActionGroup:
QActionGroup * OperationGroup;
In the constructor of MainWindow, I initialize OperationGroup and then attempt to add the two actions to it:
OperationGroup = new QActionGroup( this ); OperationGroup->addAction( ui->toolBar->findChild<QAction*>( "actionManual" ) ); OperationGroup->addAction( ui->toolBar->findChild<QAction*>( "actionAutomatic" ) );
Since the menu and toolbar are set up in Designer, I need to extract them by their names, but this does not seem to be correctly done as the program crashes at runtime with the following:
QObject::connect: Cannot connect (nullptr)::triggered() to QActionGroup::_q_actionTriggered()
QObject::connect: Cannot connect (nullptr)::changed() to QActionGroup::_q_actionChanged()
QObject::connect: Cannot connect (nullptr)::hovered() to QActionGroup::_q_actionHovered()How do I correctly get a pointer or reference to each action?
-
Within a half hour of posting this, I stumbled upon the answer. I was thinking things were more complicated than they really were, and adding them to the group is as simple as this:
OperationGroup->addAction( ui->actionManual ); OperationGroup->addAction( ui->actionAutomatic );
-