PDA

View Full Version : How to define a function body to receive external slots?



tonnot
19th April 2011, 08:40
I'm of a dead point with this.

I have a slot defined into a class A.

public slots:
void function(long value);
I have a function into external class B and I want to receive this slot (and I 'd want to receive a slot from any class. )

I dont know how to define the function body to receive this?
If this SLOT are using a function that receives a parameter, how have I to define it into this class B function ?
I have a private Qtimer into class B and I want to connect it to external functions.
I hope I have explain my problem.
Thanks

high_flyer
19th April 2011, 09:27
What do you mean with "receive" a slot?
You can "receive" a signal, but you have to call a slot...

mcosta
19th April 2011, 09:29
Sorry,

but I don't understand.
What do you mean with "receive a slot"??

Are you sure tha you're not confusing slots with signals?

tonnot
19th April 2011, 09:37
I have ClassA with a slot (defined to connect to a signal);
I have a fuction into ClassB that 'receive' this slot, does some things and depending on the result creates a Qtimer and connects it(timer.signal(timeout)) with the slot .
And I dont know how to write the body of this function to receive a SLOT and later using it to connect to Qtimer::signal(timeout).
Thanks

high_flyer
19th April 2011, 09:42
I still don't quite get what you want, it sounds like you want to pass a function pointer over a signal, which is possible, but it all sounds very bad.
Maybe if you explain what you want to have (in terms of functionality, NOT implementation) we can help you with a better implementation.

mcosta
19th April 2011, 09:53
I have a fuction into ClassB that 'receive' this slot, does some things and depending on the result creates a Qtimer and connects it(timer.signal(timeout)) with the slot .

I still don't understand what do you mean for 'receive'.

You can connect a signal with the slot if you've a pointer/reference to object aof Class A


connect(timer, SIGNAL(timeout()), aPointer, SLOT(function())

tonnot
19th April 2011, 09:54
I want to have a class with (among other things) a generic function that after some calcs conects a Qtimer with external function.
I have to declare this external function as a SLOT isn't ? Now, how can I pass this SLOT to the function I want to write ?


class A

public slots:
void actualizamem(long value);
and I have another function:

ClassB().w_timer_mem(500,1000, this, SLOT(A:actualizamem(long)));
And at aclass B:

void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
if (value <1000)
{
QTimer *a_timer = new QTimer();
a_timer->setInterval(interval);
QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
a_timer->start();
}

It does not work for me:

Thanks

high_flyer
19th April 2011, 10:05
I want to have a class with (among other things) a generic function that after some calcs conects a Qtimer with external function.
Nothing special there.

I have to declare this external function as a SLOT isn't ?
only if you want to connect it to signals - do you?

Now, how can I pass this SLOT to the function I want to write ?
Again, explain WHY you want to pass it, then we'll see if this is at all needed!
What is the reason you want to pass a slot pointer?
Btw, what you are after is called a call back function, which will rarely be needed with Qt because of signals/slots - and it seems you are trying to mix the two.

EDIT:
I think I understand now.
You want dynamic signal/slot connections.
Have a look here:
http://doc.qt.nokia.com/qq/qq16-dynamicqobject.html

tonnot
19th April 2011, 10:35
Again:
This line of code writen intro myclass A compiles (but does not work, parenthesis expected ? )
QObject::connect(ClassB().a_timer,SIGNAL(timeout), this,SLOT(A::actualizamem(long)));

As you can see I only want to connect a timer with a function, and I want to have all the control of timer on class B, becasue of it I want to convert the last line of code to :


void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
if (value <1000)
{
QTimer *a_timer = new QTimer();
a_timer->setInterval(interval);
QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
a_timer->start();
}

But I dont know how to write the body of this function neither how to call it .
Thanks four your patience.

wysota
19th April 2011, 10:57
And if you do:

ClassB().w_timer_mem(500,1000, this, SLOT(actualizamem(long)));

does it work?

mcosta
19th April 2011, 11:04
You solution is good but in its code



void ClassB::w_timer_mem(long value, int interval, QObject *parent, char * const method) {
if (value <1000)
{
QTimer *a_timer = new QTimer();
a_timer->setInterval(interval);
QObject::connect(a_timer, SIGNAL(timeout()), parent, method);
a_timer->start();
}

he try to connect a signal without parameters with a slot with one parameter. I think isn't possible

tonnot
19th April 2011, 11:09
No, I have :

Incompatible sender/receiver arguments
QTimer::timeout() --> MainWindow::actualizamem(long)

Thanks WY

wysota
19th April 2011, 11:15
Well, obviously signatures have to match. What value would you expect the slot to receive as its parameter? 4? 8? 15? 16? 24? 42?

tonnot
19th April 2011, 11:22
As Mcosta said maybe the parameter is the guilty.
If I use a function without parameters , it works.
But I'd like to can send a parameter, simply a long value, and I dont know if it is possible.
Thanks

FelixB
19th April 2011, 11:25
you connect the signal to a slot without parameters. in this slot, you create your "long" value. then you call a function and pass this value as parameter.