Solved Problem connecting QAction to slot
-
Hi. I am trying to use a QToolBar to navigate through a QStackedWidget, but I cannot seem to connect the signals.
I haveQAction* ToolBar::addNavigationButton(QString name, QStackedWidget* workspace, int index) { QAction* action = new QAction(this); action->setObjectName(name); action->setCheckable(true); actionGroup.addAction(action); connect(action, &QAction::triggered, this, [=]() {workspace->setCurrentIndex(index);}); return action; }
And at the connect-line QtCreator says: QAction::triggered is not a signal (clazy-connect-non-signal).
What am I missing. is QAction::triggered not a signal? -
@Jakob-Clausen
This is not an error from the compiler, it's from clazy. And it's wrong, you have correct signal, it compiles. So ignore clazy! -
Nothing happens when I push the button.
-
@Jakob-Clausen
Put aqDebug()
into the slot lambda? -
No. Nothing happens.
-
@Jakob-Clausen
I have not used a checkableQAction
. But I'm thinking it does not firetriggered()
, it fireschanged()
instead? Because it no longer triggers an "action"? Check the docs/comment out thesetCheckable(true)
. -
If I comment out setCheckable() it gives the same result.
I have also tried to connect changed(), and it does not work either.If I use a pushbutton instread, then I can make it work
Button* button = new Button(this, name); button->setCheckable(true); connect(button, &Button::pressed, this, [=]() {workspace->setCurrentIndex(index);}); return addWidget(button);
Is this a bad way to do it?
I guess I manually have to uncheck the previous button that was checked. -
@Jakob-Clausen Isn't https://doc.qt.io/qt-5/qaction.html#toggled what you're looking for?
-
Same result.
-
@Jakob-Clausen
Since it ought to work, and we are about to give up :), can you test it without thatQActionGroup
, just in case...? Else test it in a tiny, standalone. -
@Jakob-Clausen said in Problem connecting QAction to slot:
Same result.
Maybe a silly question, but did you add the action to a widget?
Extract from QAction documentation:Note that an action must be added to a widget before it can be used; this is also true when the shortcut should be global (i.e., Qt::ApplicationShortcut as Qt::ShortcutContext).
-
@KroMignon Maybe so. But that was it. If I add it, then it works. Clazy still complains, and I don't know what that means. But now its working. :-)
Thank you very much. -
@Jakob-Clausen said in Problem connecting QAction to slot:
Clazy still complains
The Clazy complain seems to be a bug in Clazy/QtCreator ==> https://bugreports.qt.io/browse/QTCREATORBUG-25165