Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Comunication between threads

Comunication between threads

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bruschetta
    wrote on last edited by
    #1

    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 ;)

    raven-worxR 1 Reply Last reply
    0
    • B Bruschetta

      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 ;)

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      B 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @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.

        B Offline
        B Offline
        Bruschetta
        wrote on last edited by
        #3

        @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 :)

        raven-worxR 1 Reply Last reply
        0
        • B Bruschetta

          @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 :)

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Bruschetta

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

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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          B 1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @Bruschetta

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

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

              B Offline
              B Offline
              Bruschetta
              wrote on last edited by
              #6

              @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= ()
              =================================================
              
              kshegunovK 1 Reply Last reply
              0
              • B Bruschetta

                @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= ()
                =================================================
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

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

                Read and abide by the Qt Code of Conduct

                B 1 Reply Last reply
                0
                • kshegunovK kshegunov

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

                  B Offline
                  B Offline
                  Bruschetta
                  wrote on last edited by Bruschetta
                  #8

                  @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?

                  kshegunovK 1 Reply Last reply
                  0
                  • B Bruschetta

                    @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?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    @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.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved