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. Thread and Socket
QtWS25 Last Chance

Thread and Socket

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 530 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    This is a work in progress, so I please don't expect it to be completed finished code:

    void SckThread::run()
    {
        QTcpSocket* pSocket = new QTcpSocket();
    
        if ( pSocket == nullptr ) {
            return;
        }
        //Set the ID
        if( !pSocket->setSocketDescriptor(msckDescriptor) )
        {
        //Something's wrong, we just emit a signal
            emit error(pSocket->error());
            return;
        }
        //Connect socket and signal
        pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
        QEventLoop evtLoop;
        //pSocket->moveToThread(this->thread());
        mpsckIncoming = pSocket;
        auto conn1 = QObject::connect(mpsckIncoming
            ,QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error)
            ,this, &SckThread::onErrorOccurred);
        auto conn2 = QObject::connect(mpsckIncoming, &QAbstractSocket::disconnected
            ,&evtLoop, [&evtLoop]() {
            qinf() << "Client disconnected";
            evtLoop.quit();
        });
        auto conn3 = QObject::connect(mpsckIncoming, &QAbstractSocket::readyRead
            ,this, &SckThread::onReadyRead);
        //We'll have multiple clients, we want to know which is which
        qinf() << "Client connected";
        //Make this thread a loop,
        //thread will stay alive so that signal/slot to function properly
        //not dropped out in the middle when thread dies
        evtLoop.exec();   
        QObject::disconnect(conn1);
        QObject::disconnect(conn2);
        QObject::disconnect(conn3);
        //Cleanup
        delete pSocket;
    }
    

    I'm getting a message up in the Application Output:

    QObject: Cannot create children for a parent that is in a different thread.
    

    I'm not sure why and as you can see in the function I've tried commenting out the line:

    pSocket->moveToThread(this->thread());
    

    Still occurs, with this line uncommented.

    Kind Regards,
    Sy

    KroMignonK 1 Reply Last reply
    0
    • KroMignonK KroMignon

      @SPlatten said in Thread and Socket:

      Still occurs, with this line uncommented.

      I told you many and many times not to sub-class QThread but instead use "worker class".
      There is a rule with Qt: Do not sub-class QThread when using with QObject based instances

      ==> I would be great, at least for you, if you could accept this! (cf. https://www.vikingsoftware.com/how-to-use-qthread-properly/)

      Do it on the right way, and you will resolve your issue.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #3

      @KroMignon , I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.

      Kind Regards,
      Sy

      KroMignonK 1 Reply Last reply
      0
      • SPlattenS SPlatten

        This is a work in progress, so I please don't expect it to be completed finished code:

        void SckThread::run()
        {
            QTcpSocket* pSocket = new QTcpSocket();
        
            if ( pSocket == nullptr ) {
                return;
            }
            //Set the ID
            if( !pSocket->setSocketDescriptor(msckDescriptor) )
            {
            //Something's wrong, we just emit a signal
                emit error(pSocket->error());
                return;
            }
            //Connect socket and signal
            pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
            QEventLoop evtLoop;
            //pSocket->moveToThread(this->thread());
            mpsckIncoming = pSocket;
            auto conn1 = QObject::connect(mpsckIncoming
                ,QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error)
                ,this, &SckThread::onErrorOccurred);
            auto conn2 = QObject::connect(mpsckIncoming, &QAbstractSocket::disconnected
                ,&evtLoop, [&evtLoop]() {
                qinf() << "Client disconnected";
                evtLoop.quit();
            });
            auto conn3 = QObject::connect(mpsckIncoming, &QAbstractSocket::readyRead
                ,this, &SckThread::onReadyRead);
            //We'll have multiple clients, we want to know which is which
            qinf() << "Client connected";
            //Make this thread a loop,
            //thread will stay alive so that signal/slot to function properly
            //not dropped out in the middle when thread dies
            evtLoop.exec();   
            QObject::disconnect(conn1);
            QObject::disconnect(conn2);
            QObject::disconnect(conn3);
            //Cleanup
            delete pSocket;
        }
        

        I'm getting a message up in the Application Output:

        QObject: Cannot create children for a parent that is in a different thread.
        

        I'm not sure why and as you can see in the function I've tried commenting out the line:

        pSocket->moveToThread(this->thread());
        

        Still occurs, with this line uncommented.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @SPlatten said in Thread and Socket:

        Still occurs, with this line uncommented.

        I told you many and many times not to sub-class QThread but instead use "worker class".
        There is a rule with Qt: Do not sub-class QThread when using with QObject based instances

        ==> I would be great, at least for you, if you could accept this! (cf. https://www.vikingsoftware.com/how-to-use-qthread-properly/)

        Do it on the right way, and you will resolve your issue.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        SPlattenS 1 Reply Last reply
        6
        • KroMignonK KroMignon

          @SPlatten said in Thread and Socket:

          Still occurs, with this line uncommented.

          I told you many and many times not to sub-class QThread but instead use "worker class".
          There is a rule with Qt: Do not sub-class QThread when using with QObject based instances

          ==> I would be great, at least for you, if you could accept this! (cf. https://www.vikingsoftware.com/how-to-use-qthread-properly/)

          Do it on the right way, and you will resolve your issue.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @KroMignon , I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.

          Kind Regards,
          Sy

          KroMignonK 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @KroMignon , I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #4

            @SPlatten said in Thread and Socket:

            I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.

            I am not sure you really solve the problem, but it is your choice.
            I can not understand why you always do same errors, ask for help and then refuse to accept advice that are given from others.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            SPlattenS 1 Reply Last reply
            5
            • KroMignonK KroMignon

              @SPlatten said in Thread and Socket:

              I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.

              I am not sure you really solve the problem, but it is your choice.
              I can not understand why you always do same errors, ask for help and then refuse to accept advice that are given from others.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              @KroMignon , Can you accept that you might not know it all?

              There are numerous examples of sub-classing QThread and sub-classing is standard OOP.

              Kind Regards,
              Sy

              JKSHJ KroMignonK 2 Replies Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                And you doing it wrong no matter how often we tell you how to do it right...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • SPlattenS SPlatten

                  @KroMignon , Can you accept that you might not know it all?

                  There are numerous examples of sub-classing QThread and sub-classing is standard OOP.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #7

                  @SPlatten said in Thread and Socket:

                  There are numerous examples of sub-classing QThread

                  https://doc.qt.io/qt-5/qthread.html#details : "new slots should not be implemented directly into a subclassed QThread."

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

                  1 Reply Last reply
                  3
                  • SPlattenS SPlatten

                    @KroMignon , Can you accept that you might not know it all?

                    There are numerous examples of sub-classing QThread and sub-classing is standard OOP.

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #8

                    @SPlatten said in Thread and Socket:

                    Can you accept that you might not know it all?

                    Yes, I can. I am not a C++ expert, nor a Qt expert.

                    There are numerous examples of sub-classing QThread and sub-classing is standard OOP.

                    Of course, sub-classing is OOP basic, but that's not the point here.

                    What I try to tell you is that sub-classing QThread is in general the wrong way to do.
                    Especially when using with QObject.
                    That is not my point of view, but Qt recommendation, which is the company which have create all those code.

                    I have tried to explain it in many ways, I give you links to Qt documentation, to blogs which explains you why it is wrong.
                    But you don't want to learn.

                    Again, your choice.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    3

                    • Login

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