Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. [SOLVED] Problem with Signals in QThread.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with Signals in QThread.

Scheduled Pinned Locked Moved Qt Creator and other tools
12 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andnerb
    wrote on last edited by
    #3

    Hello,

    I need your help.
    Actually, I'm codding a multithreaded server but, I'm encounter an issue and I don't know why.
    Perhaps you can help me.

    My_MainWindow_Class:

    @void MainWindow::incomingConnection()
    {
    int socketDescriptor;
    string connected_nbr = std::to_string(g_conected_nbr + 1);

    g_conected_nbr += 1;
    socketDescriptor = 0;
    cout << "Connecting : " << socketDescriptor << endl;
    ui->textBrowser_2->setText("Connected (" + QString(connected_nbr.c_str()) + " client)");
    my_soc = my_srv.nextPendingConnection();
    MyThread *new_thread = new MyThread(my_soc->socketDescriptor());
    connect(new_thread, SIGNAL(client_disconnected()), this, SLOT(delete_client()));
    connect(new_thread, SIGNAL(finished()), new_thread, SLOT(deleteLater()));
    new_thread->start();
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andnerb
      wrote on last edited by
      #4

      My_Kill Thread method

      @void MyThread::kill_thread()
      {
      emit client_disconnected();
      std::cout << "Disconnected !" << std::endl;
      my_soc->deleteLater();
      }@

      My_Thread Run Method:

      @
      void MyThread::run()
      {
      std::cout << "Socket n"<< socketDesciptor << ": Starting..." << std::endl;
      my_soc = new QTcpSocket();
      if (!my_soc->setSocketDescriptor(this->socketDesciptor))
      {
      return;
      }
      connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()));
      connect(my_soc, SIGNAL(disconnected()), this, SLOT(kill_thread()));
      std::cout << "Client Connected !" << std::endl;
      exec();
      }@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andnerb
        wrote on last edited by
        #5

        The problem in this code:

        The signal client_disconnected() is never emitted by the thread to my MainWindow class.
        Consequence:
        @connect(new_thread, SIGNAL(client_disconnected()), this, SLOT(delete_client()));@

        Do nothing.

        Please help me, I don't now why this signal isn't emited :/ because in my Thread connections work fine !

        Thanks a lot.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andnerb
          wrote on last edited by
          #6

          Here is My_Thread.h code:

          @class MyThread : public QThread
          {
          Q_OBJECT

          public:
          explicit MyThread(int ID);
          ~MyThread();
          void run();

          signals:
          void client_disconnected();

          public slots:
          void kill_thread();
          void tcp_receve_commands();

          private:
          QTcpSocket *my_soc;
          int socketDesciptor;
          QString old_question;

          };@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andnerb
            wrote on last edited by
            #7

            Here is includes above My_Thread.h code (because forum block my post when i add them)

            @#include "mainwindow.h"
            #include "ui_mainwindow.h"
            @

            Sorry for multiples posts but Qt forum consider my entire post like a spam.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Hi and welcome to devnet,

              Are you sure your socked is getting deconnected properly ?

              You can also check your implementation against the "Threaded Fortune Server" example

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #9

                Hi,

                [quote author="andnerb" date="1422204990"]
                @
                connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()));
                @
                [/quote]tcp_receive_commands() (and all of MyThread's slots) will end up running in the main thread. I presume you don't want this.

                The "QThread documentation":http://doc.qt.io/qt-5/qthread.html says (emphasis added):

                • "It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread."

                If you need signals and slots to communicate with your thread, don't subclass QThread. Use a worker object instead (see the QThread documnetation for an example)

                [quote author="SGaist" date="1422223035"]You can also check your implementation against the "Threaded Fortune Server" example[/quote]The Threaded Fortune Server (TFS) is designed very differently from andnerb's code. TFS runs a "single-shot" thread every time a new connection comes in, and the thread quits immediately after sending one message. There is no event loop or custom slots.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Indeed both are different, I was just suggesting to compare both implementation in the case he was looking for a workflow similar to the TFS but missed the exemple.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andnerb
                    wrote on last edited by
                    #11

                    Ok, thanks a lot for your answers !
                    I will see QThread documentation, with worker class.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andnerb
                      wrote on last edited by
                      #12

                      I've an other Question:

                      After create my_worker and try to move it in my QThread I got this error during execution:

                      @QObject: Cannot create children for a parent that is in a different thread.
                      (Parent is QNativeSocketEngine(0x15d345b8), parent's thread is QThread(0x15cfdc78), current thread is QThread(0x15d345d8).@

                      And worker stop to work...

                      I don't know why ?
                      Is it problem with my QTcpServer ?

                      I haven't any exec method, because Worker herit from QObject and My Worker stop to work after one request attempt to my server.

                      EDIT :

                      Problem solved:

                      I'm using :

                      @connect(my_soc, SIGNAL(readyRead()), this, SLOT(tcp_receve_commands()), Qt::DirectConnection);
                      connect(my_soc, SIGNAL(disconnected()), this, SLOT(kill_thread()), Qt::DirectConnection);@

                      Now, because without Qt::DirectConnection, connections aren't executed in the Thread.

                      Thanks a lot all for your help, my problem is solved !
                      I love Qt ^^ !

                      1 Reply Last reply
                      0

                      • Login

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