Can Qt signals return a value ?

Can Qt signals return a value ?

Asked on December 21, 2018 in Qt.
Add Comment


  • 3 Answer(s)

    Get the  value from the slot  signal was connected . But the problem was only return the  last return value from the multiple connected slots:

     Design the class definition (main.cpp):

    #include <QObject>
    #include <QDebug>
     
    class TestClass : public QObject
    {
        Q_OBJECT
    public:
        TestClass();
     
    Q_SIGNALS:
        QString testSignal();
     
    public Q_SLOTS:
        QString testSlot1() {
        return QLatin1String("testSlot1");
        }
       QString testSlot2() {
       return QLatin1String("testSlot2");
       }
    };
     
    TestClass::TestClass() {
       connect(this, SIGNAL(testSignal()), this, SLOT(testSlot1()));
       connect(this, SIGNAL(testSignal()), this, SLOT(testSlot2()));
     
       QString a = emit testSignal();
       qDebug() << a;
    }
     
    int main()  {
        TestClass a;
    }
     
    #include "main.moc"
    

    Constructs one of the test classes. The constructor wires up two slots to the testSignal , and then emits the signal. It captures the return value from the slots.

    Expand  the code”testSlot2″, and return the  last value from the connected slots of the signal.

    Qt Signals are a syntax sugared interface to the signaling pattern. Slots are the recipients of signal and direct connected signal-slot relationship.

    foreach slot in connectedSlotsForSignal(signal):
    value = invoke slot with parameters from signal
    return value
    

    The  moc  as little more to help in this process rudimentary type checking, etc, but this helps in the  paint picture.

    Answered on December 21, 2018.
    Add Comment

    The answer is given below:

           Boost::signals are completly  different  in Qt. The former provide an advanced callback mechanism, whereas the latter implement the signaling idiom. In the context of multithreading, Qt’s cross-threaded signals depend on message queues, so they are called asynchronously at some unknown to the emitter’s thread point in time.

    Answered on December 21, 2018.
    Add Comment

    The qt signal return value:

    In qt_metacall function returns an integer status code. Because of an actual return value impossible unless you fudge around with the meta object system and moc files after precompilation.

    The normal function of  parameters at assets they should be possible to update the code in  a way to use “out” parameters that act as the “return”.

    void ClassObj::method(return_type * return_)
    {
       ...
       
       if(return_) *return_ = ...;
    }
     
    // somewhere else in the code...
     
    return_type ret;
    emit this->method(&ret);
    

     

    Answered on December 21, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.