[Solved] What is the signature of a function used as SLOT
-
I was wondering what signature (type of the parameter theSlot) must be used for function that will be used as a slot:
I know that connect actually gets the slot as a simple const char* but the following example does not work and the compiler reports theSlot as an unreferenced parameter:
@
void addMenuItem(QMenu* theMenu, const QString& theTitle, const char* theSlot)
{
QAction* aAction = new QAction(theTitle, this);
connect(aAction, SIGNAL(triggered()), this, SLOT(theSlot));
theMenu->addAction(aAction);
}void buildMenu(QMenu* theMenu)
{
addMenuItem(theMenu, "Open file...", "slot_open_file()");
}
@ -
You have missunderstood the thing a bit. If you use SLOT (which is a kind of macro understood by the moc compiler) it should be a method name and its parameter types without quotes. However connect's 4th parameter has type const char*, so in your sample the SLOT is not required.
-
You can read more about it "here":http://doc.qt.nokia.com/4.7/signalsandslots.html
-
I played around a little and actually the const char* parameter type and the use of SLOT was OK but I used slot on the wrong argument.
The following example does work as expected:
@
void addMenuItem(QMenu* theMenu, const QString& theTitle, const char* theSlot)
{
QAction* aAction = new QAction(theTitle, this);
connect(aAction, SIGNAL(triggered()), this, theSlot);
theMenu->addAction(aAction);
}void buildMenu(QMenu* theMenu)
{
addMenuItem(theMenu, "Open file...", SLOT(slot_open_file()));
}
@ -
That is the correct usage. Then you don't care how SLOT is done :-)