I'm currently working on a game with a friend of mine, and now we are kind of stuck. We need to pass two arguments to a slot. I want to use one slot for two buttons, and one of the buttons will be used for adding, and the other one for subtracting. This will be one of the arguments, either 0 (for subtracting) or 1 (for adding). The other argument will be a kind of ID, because i will have several sets of these two buttons. I've used several other slots in my code, and on these slots I've been using QSignalMapper like this:
Button * button = new Button(argument1, argument2, argument3);
int num = 1;
QSignalMapper * signalMapper = new QSignalMapper(this);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map)));
signalMapper->setMapping(button, num);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(mySlot(int)));
scene->addItem(button);
Is there any way I can pass two arguments to a slot?
Use the sender()
function instead:
void mySlot()
{
if (sender() == addButton)
...
else
...
}
In your case, you can remove the int
argument from mySlot
and do the following:
connect(addButton, SIGNAL(clicked()), someObject, SLOT(mySlot()));
connect(subButton, SIGNAL(clicked()), someObject, SLOT(mySlot()));
Then use the sender
function to determine the source.
To directly answer your question, yes, you can define a slot that accepts up to 8 arguments (pre C++11) or any number (C++11 or later). The issue is that they must be connected to a signal with as many or more parameters.
Example, if you have a signal with the signature notify(int, bool, QString)
you can connect it to a slot with any of the following signatures:
someSlot(int)
someSlot(int, bool)
someSlot(int, bool, QString)