PDA

View Full Version : Slot



mickey
3rd June 2006, 12:08
Hi I coded this connection, but I need to do this: float type = setBox(int)); How? thanks


connect(bgroup[i], SIGNAL(clicked(int)), this, SLOT(setBox(int)));

munna
3rd June 2006, 13:11
Slots are just normal functions.

Delare your slot as

float setBox(int );

and then use as normal function whereever you want.

mickey
3rd June 2006, 13:26
sorry, I just declare it so. I need to do that connect and togheter assing its returned value to var type..........

munna
3rd June 2006, 13:33
I need to do that connect and togheter assing its returned value to var type..........

Can you explain this ??

mickey
3rd June 2006, 14:00
that connect is right; it's works; but it return a float that I need to assign to a a variable;
I can't call setBox() is somewhere: it must return a value only when its signal occurs....

munna
3rd June 2006, 14:21
In that case your slot setBox(int) should emit a signal with your return value as a parameter and this signal will be connected to a slot in your bgroup[i].

I hope you understand what i mean.

mickey
3rd June 2006, 15:28
is there a way simpler? SLOT must be declared with a return value (eg void). I thought there's a sintax to assign its returned value to a variabile...

patrik08
3rd June 2006, 16:49
is there a way simpler? SLOT must be declared with a return value (eg void). I thought there's a sintax to assign its returned value to a variabile...

Why? you must not declare return value ... on slot ...is possible to grab all data from GUI... if you set..
http://doc.trolltech.com/4.1/signalsandslots.html




**.header
--------------------- to call on emit method ....
/* emits */
signals:
void status( const QString &);
void ConnectorSuccess();
void SendLine();
void ErrorCloseAll();
void SuccessQuit();
/* normal slot */
private slots:
void disconnected();
void connected();
void ReadLiner();
void PutSendLine();
---------------------


linecmd = 34,56; /* save on class int .or float ... .. */
linesend = 8; /* save on class int .or float ... .. */
emit SendLine(); /* switsch all case.... or grab int or float direct from GUI */


void Smtp::PutSendLine()
{
int current = linesend;
grab data ... linecmd
qDebug() <<"### Go and Send line " << linesend;
switch(current) {
case 1:
response = SendLineAndGrab("ehlo localhost");
if (response.size() > 0) {
ErrorMSG.append(response);
qDebug() << "1---- " << response;
linesend = 2;
emit SendLine();
} else {
qDebug() << "Connection loost";
}
response ="";
break;
case 2:
response = SendLineAndGrab("AUTH LOGIN");
if (response.size() > 0) {
ErrorMSG.append(response);
qDebug() << "2---- " << response;
linesend = 3;
emit SendLine();
} else {
qDebug() << "Connection loost";
}
response ="";
break;
case 3:
.......................
}

mickey
3rd June 2006, 17:51
I'm still confused; can anyone learn me why QT permits to declare a SLOT in that way? (this compile). thanks


public slots:
float setBox(int);

fullmetalcoder
3rd June 2006, 21:30
It is allowed because slots are normal members functions... What allows connection is some extra meta-data (character strings with Qt meta object system) that is generated by moc and handled by signal/slots system. When a given signal is caught by event handlers all the connected slots (or signals as well BTW) are called one by one thus their return value can't be caught by the user... Emitting another signal is definitely the simplest way!

mickey
4th June 2006, 03:44
Sorry, I don' t still understand how use a signal...this is what i need do.....


float setValBox(int i) {
float ii;
..........
return ii;
}

float type;
connect(bgroup[i], SIGNAL(clicked(int)), this, SLOT(type=setValBox(int)));

How use signal; for what? thanks

fullmetalcoder
4th June 2006, 09:34
signals are declared in headers that way :


class X : public QObject
{
Q_OBJECT

signals:
void mySignal( /*Place there the parameters as for a normal function*/ );
}

signals are basically private function (or protected I'm not sure...) whose implementation is made by moc. You can call them just as normal functions but it's better to use the "emit" keyword before them to make it clear that they are signals... BTW "emit" is just an empty macro AFAIK

This code may look pretty ugly because I don't know exactly what you wanna do and how your classes interact...


class X : public QObject
{
Q_OBJECT

...

public slots:
void setValBox(int val);

private slots:
void setValBox(float return);

signals:
void valBoxSet(float return);

private:
float returnValue;
}

...

X::X()
{
connect(bgroup[i], SIGNAL( clicked(int) ),
this , SLOT ( setValBox(int) );

connect(this, SIGNAL( valBoxSet(float) ),
this, SLOT ( setValBox(float) );
}

void X::setValBox(int)
{
float ii;
...
emit setValBox(ii);
}

void X::setValBox(float f)
{
returnValue = f;
}

mickey
4th June 2006, 13:10
Hi, but for my aims this can be OK. The problem was that I don't want declare returnValue as class member, but only a local variabile........I hpe you understand my problem...

signals are declared in headers that way :]


class X : public QObject
{
Q_OBJECT
public slots:
void setValBox(int val);
//private slots:
// void setValBox(float return);
//signals:
//void valBoxSet(float return);
private:
float returnValue;
}

X::X() {
connect(bgroup[i], SIGNAL( clicked(int) ),
this , SLOT ( setValBox(int) );
}

void X::setValBox(int val) {
if (val == 0) returnValue = 1.0; //val has to be 0 or 1
else returnValue = 0.0;
}


but what's diference between those:


void setValBox(float);
void setValBox(float return);

vdboor
4th June 2006, 13:18
A slot can't have a return value because you can attach multiple slots (=event-handlers) to the same signal (=event). Which one would Qt have to return? Besides, slots can run at a later time in the event queue, when your original function already ended.

It's recommended to find a different design that doesn't need return values. In your example, I'd rather use an interface:

First you have a base class that defines the interface all subclasses should confirm to.

class BoxHandler {
virtual float setBox(int) = 0;
}

Then you can define multiple versions of your box function

class BoxImplementation1 : public BoxHander {
virtual float setBox(int);
}

class BoxImplementation2 : public BoxHander {
virtual float setBox(int);
}

And then code each method:

float BoxImplementation1::setBox(int x) { ... }
float BoxImplementation2::setBox(int x) { ... }

And the original class can use any reference, as long as it implements the BoxHandler interface:

void demo( BoxHandler *handler )
{
float y = handler->setBox(x);
}
The C++ runtime will automatically know whether it calls the first or second function:


void caller()
{
BoxImplemtation2 handler;
demo( handler );
}

Off course, the BoxImplementation classes may also be part of your normal classes:


class MyApplication : public QApplication, public BoxHandler {
...
}
Making the call something like:

void caller()
{
demo( myApplicationObject );
}