PDA

View Full Version : Can we connect QTimer::SingleShot with a slot taking arguments?



maverick_pol
17th September 2008, 18:30
Hi all,

I have a short question:

Is it possible for this kind of connection to work:
(Assume that all we need is defined)

QTimer::singleShot(1000,this,SLOT(vSlot(2,3)));

I know that it is possible to connect a QTimer::singleShot(...) with a slot not taking any args, but Can we do the same with a slot taking arg?
Could you explain why this is not possible( or maybe is) ?

Thank you.

jpn
17th September 2008, 18:37
No, it is not possible. QObject::connect() does not save any state, you cannot map parameter values like that. Imagine it as a function call from the signal to the slot. The only way you can do it directly is the same way you can do it with a direct function call; with default parameter values:


void vSlot(int foo = 2, int bar = 3);

QTimer::singleShot(1000,this,SLOT(vSlot()));

maverick_pol
17th September 2008, 18:49
Hi,

I suppose we have a small misunderstanding.
We have this code:

myClass.h

....
private slots:
void mySlot(int);
...
myClass.cpp

..
void MyClass::mySlot(int _value){
qDebug()<<_value;
}
....
..... and somewhere in the code.....

int x = 3;
QTimer::singleShot(2000,this,SLOT(mySlot(x)));
...

What I am trying to explain is this:
In my app a QTimer::singleShot(...) do not work with slots taking arguments.

And I would like to ask:
Can a QTimer::singleShot() work with a slot taking arguments? yes, not? Why no?

Kacper

caduel
17th September 2008, 19:00
No, as already said, you can NOT connect a signal to a slot that has MORE arguments than the signal. (It may have less, though.)
The only exception (if you will) is when the missing arguments have defaults.

maverick_pol
17th September 2008, 19:02
Thanks.

I just forgot that using QTimer::singleShot(..) the signal sent is "timeout(...) taking no arguments and using the timeout interval". I was lucking this information.

Clear enough.