Comunication between threads



  • I have a question and not an easy one for me, let me see if i can explain it well...

    In my main page i create a thread and it's porpouse is to open a TCP connection with a server who's going to send to him (the client) some messages every few secs.
    Meantime the main thread is going to make the application run.
    So the user can open Dialog1, then Dialog2, then Dialog3 and finally Dialog4.

    Dialog4 wants to know if the thread i started at the beginning (the TCP connection one) is receiving some particular messages.
    The problem i'm facing is: How can i connect those events? Is there a way to use SIGNALS and SLOTS? I have to use another way?

    Thanks for your time and, i hope, answers and hints ;)


  • Moderators

    @Bruschetta said in Comunication between threads:

    Is there a way to use SIGNALS and SLOTS?

    Yes, you just need to make sure that you use Qt::QueuedConnection or Qt::AutoConnection parameter in the connect statement.

    For inter-thread communication using queued signals all parameters need to be declared and registered meta-types.



  • @raven-worx
    How can i implement it?

    Let say for example i have this function in the Thread

    void TCPThread::words(QString x, QString y)
    {
         emit sendTwoWords(Qstring x, Qstring y ); // My signal
    }
    

    And in the Dialog4 the funcion

    void dialog4::getTwoWords(const QString x,const QString y)
    {
        qDebug() << "Words  : " << x + " " + y; //My slot
       
    }
    

    How can i CONNECT them? If i call the connect just before the Dialog4 call i still miss the reference to the Thread i created in the mainpage.
    What i'm missing? Thanks for the patience :)


  • Moderators

    @Bruschetta

    connect( myTcpThread, &TCPThread::words, myDialog4, &dialog4::getTwoWords, Qt::AutoConnection );
    

    How and when you exactly store these 2 pointers depends on your application design.


  • Qt Champions 2016

    small note.
    When you emit the signal, you just supply an actual variable so the types are not shown

    emit sendTwoWords(Qstring x, Qstring y ); // My signal

    --->

    emit sendTwoWords( x, y ); // My signal



  • @raven-worx

    My TCPThread is generated by the GUI thread.
    Is there a way i can get the child from the GUI thread to do something like this?

    connect( QThread::currentThread()->children(), &TCPThread::words, myDialog4, &dialog4::getTwoWords, Qt::AutoConnection );
    

    The command to find the child must be something else because i get somethign like this

    Using
        qDebug() << "=================================================";
        qDebug() << "Current: threadID=" << QThread::currentThread();
        qDebug() << "Figlio:  threadID=" << QThread::currentThread()->children();
        qDebug() << "=================================================";
    
    =================================================
    Current: threadID= QThread(0x1dcaa70)
    Child:  threadID= ()
    =================================================
    

  • Qt Champions 2016

    Just make the connection wherever you create the thread and or worker object(s).



  • @kshegunov
    It could be the optimal way but i'm creating the "Listener" Thread in the MainWindow and the working objects are creating when i call Dialog4 that is son of Dialog3 that is son of Dialog2 son of dialog 1 son of MainWondow...

    This is the reason why i'm having problems calling CONNECT.
    Do you think i can use some workaround?


  • Qt Champions 2016

    @Bruschetta said in Comunication between threads:

    I believe you should really rethink your design and you should provide a snippet so we can see how you create and initialize the threads.
    On a related note:

    QThread::currentThread()->children()
    

    In 99.99% of cases this really means nothing, as QThread::currentThread() will return a QThread object which would live in the main thread (and by design all its children will be living in the main thread as well).

    Show us your (cut-down) implementation of TcpThread and how you start/initialize it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.