PDA

View Full Version : Passing arguments



papillon
6th August 2012, 16:49
Hi,
I understood it's not possible to pass extra arguments within a SLOT function.

So, given the following code:

QTimer::singleShot(PollTimeout, this, SLOT(myFunction()));

Could someone be so kind to teach me how to pass arguments to the myFunction() wrapped in the SLOT?
Thanks

yeye_olive
6th August 2012, 17:00
That is not possible. I suggest you store the information in the data members of the object whose slot will be executed. If you cannot do that (e.g. if there are several timers calling myFunction and you need to identify which one timed out), you can, instead of using QTimer::singleShot(), instanciate QTimers and use QSignalMapper to bind values (integers or pointers) to their timeout() signals to identify them.

d_stranz
7th August 2012, 19:28
I understood it's not possible to pass extra arguments within a SLOT function.

The function signature for your slot must match the function signature of the signal it is connected to, with the exception that the slot can have fewer arguments than the signal by dropping arguments from the end of the argument list. That is, if your signal has arguments (int, float, double), then valid slots could have the signatures (), (int), (int, float), and (int, float, double). But a slot with signature (int, float, double, char *) would be rejected at connect time.

The way around this is usually to create a slot that is connected to the first signal, and in that slot emit a second signal that has the correct arguments. You need to store the values for the additional arguments somewhere, typically as member variables of the class that handles the first signal.

papillon
8th August 2012, 23:44
Thank you very much for the suggestions!