Unsolved connect action to slot new syntax
-
Hi,
I am trying to connect an action that's created in the designer to a slot.
The action is actionLogout and can be called via ui->actionLog_out where ui is from MainWindow.
The slot is logout().I already tried this:
logoutAct = new QAction(tr("logout")); connect(this, logoutAct, this, &MainWindow::logout);
it says: "no matching member function for call to connect"
-
Hi
You seem slightly confused about the syntax
It is
Who has the signal, the signal , Who has the Slot, the SlotlogoutAct = new QAction(tr("logout"));
connect(logoutAct, &QAction::Triggered, this, &MainWindow::logout);so we have
logoutAct (who) , &QAction::Triggered (the signal ) , this (who), &MainWindow::logout (slot) -
@hobbyProgrammer
One thing to note: as per @mrjj's reply, you should (actually must) use either the oldSIGNAL
/SLOT()
syntax (deprecated) or the new non-macro ones (recommended) like @mrjj's. Don't even try mixing them as you have! Stop using eitherSIGNAL()
orSLOT()
from now onward! -
@mrjj thank you, at least it doesn't come with errors now, but it doesn't respond to me clicking 'logout' in the menubar.
I tried:
connect(ui->actionLog_out, &QAction::triggered, this, &MainWindow::logout);
and
QAction *logoutAct = ui->actionLog_out; connect(logoutAct, &QAction::triggered, this, &MainWindow::logout);
-
@hobbyProgrammer
If you have just @mrjj's code it won't. That creates an "anonymous"/"standalone"QAction
. You have to connect theQAction
to the appropriate item on theQMenu
, else they don't have anything to do with each other. I don't know if or how the Designer does that. And make sure you put a breakpoint or output something in whatever yourMainWindow::logout
is, so you are sure whether it is called or not. -
I updated my example..
https://github.com/DeiVadder/LoginExample -
@J-Hilk Looks like you've got a full-time job there :)
-
@JonB If it's only taking a couple of seconds/minutes 🤷♂️
;)