Question

I'm new to Qt programming. I get the hang of emitting a signal, and catching the signal with a slot, but is it possible to call the slot as a regular method?

for example, in the following code snippet:

class someClass{
..
..
//method
    void emitsig1(int val)
    {
      emit sig1(val);
    }
public signals:
    void sig1(int a);
};

class someOtherClass{
..
..   
public slots:

    int onSig1(int a)
    {
    //some computation on a
    return a;
    }
};

int main(argc, char** argv){
..
..

someClass obj1 = new someClass();
someOtherClass obj2 = new someOtherClass();
int value = 10, result =0;
obj1.emitsig1(value);

QObject::connect(obj1, SIGNAL(sig1(int), obj2, SLOT(onSig1(int)));

int newvalue = 100;

//is it legal to do this, and if so, what value should I expect "result" to have?
result = obj2.onSig1(newvalue);
Was it helpful?

Solution

Sure.

Did you try it ?

In Qt, you have a signal/slot system. Signals have to be executed using emit, but slots are ordinary functions (declared as slots) that can be executed automatically after a signal is emitted, when they are connected with the connect function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top