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. Help needed for using QTcpSocket in a QThread
Forum Updated to NodeBB v4.3 + New Features

Help needed for using QTcpSocket in a QThread

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 2.1k 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.
  • S Offline
    S Offline
    srikanth.koorma
    wrote on last edited by
    #1

    Hello friends, can anyone of you please help me with the below thread code for QTcpSocket. Code works exactly but the following error is shown in application output when thread is started.

    Application Output:
    @
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x60d690), parent thread is QThread(0x793070), current thread is SerClass(0x7fffd4f8ee28)
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x60d690), parent thread is QThread(0x793070), current thread is SerClass(0x7fffd4f8ee28)
    @

    rescueop.h
    @
    #include <QMainWindow>
    #include <QThread>
    #include "ui_rescueop.h"

    namespace Ui {
    class rescueOp;
    }

    class SerClass : public QThread
    {
    Q_OBJECT

    public:
    SerClass(QObject* parent = 0);
    ~SerClass();

    protected:
    void run(void);

    private:

    signals:
    };

    class rescueOp : public QMainWindow, public Ui::rescueOp
    {
    Q_OBJECT

    public:
    rescueOp(QWidget *parent = 0);
    ~rescueOp();

    signals:

    private slots:

    private:
    SerClass thread;
    };
    @

    rescueop.cpp
    @
    #include "rescueop.h"
    #include "ui_rescueop.h"
    #include <QtGui>
    #include <QTcpSocket>
    #include <stdlib.h>
    #include <QThread>
    QTcpSocket socket;

    rescueOp::rescueOp(QWidget *parent) : QMainWindow(parent),Ui::rescueOp(),thread(this)
    {
    setupUi(this);
    welcome->setText(" Welcome to Rescue Application 1.0 ");
    }

    rescueOp::~rescueOp()
    {
    if(socket.isOpen())
    socket.close();
    if(thread.isRunning())
    thread.exit(0);
    }

    SerClass::SerClass(QObject* parent):QThread(parent)
    {
    qDebug() << " Thread constructor " << endl;
    }

    SerClass::~SerClass()
    {
    serialPort.close();
    }

    void SerClass::run()
    {
    socket.connectToHost("www.voidrealms.com",4444);
    if(socket.waitForConnected(3000))
    {
    qDebug() << "Connected !";
    serverFile.open(QIODevice::ReadWrite | QIODevice::Text);
    socket.write("Hello from client");
    socket.waitForBytesWritten(1000);
    while(socket.bytesToWrite() != 0) ;
    socket.waitForReadyRead(3000);
    qDebug() << "Reading: " << socket.bytesAvailable();
    qDebug() << socket.readAll();
    socket.close();
    }
    else
    emit display(socket.errorString());
    }
    @

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

      Hi,

      You socket is created in the constructor of your SerClass, that happens in the main thread. The run function is active in the thread managed by SerClass. So you are trying to use a socket created in the main thread in another thread.

      For what you want to do, you don't need any thread. Have a look at the Fortune examples.

      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
      • S Offline
        S Offline
        srikanth.koorma
        wrote on last edited by
        #3

        Hello SGaist, so here am using some serial communication in a thread from which I should send that data to the server by using a socket. Is there anyway that I can use that socket in a thread without any error.?

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

          There are several way. Look at the latest QThread documentation and I mean it, really, take the time to do it. Using thread even if it looks simple with Qt is a complex matter.

          If you still want to stay with re-implement run approach, create your socket in the run method.

          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
            andre
            wrote on last edited by
            #5

            Unless you really understand how threading in general, and threading in Qt in particular works, I would really recommend you stay away from subclassing QThread. Once you understand why to stay away from it, you're far enough to understand the cases where you actually can use it. This is not one of those cases. Note that I am not saying that subclassing of QThread is a bad idea in all cases, I am just saying that doing that is a bad idea while you lack the knowledge to understand the pitfalls associated with this approach.

            1 Reply Last reply
            0
            • IamSumitI Offline
              IamSumitI Offline
              IamSumit
              wrote on last edited by
              #6

              [quote author="srikanth.koorma" date="1413519671"]Is there anyway that I can use that socket in a thread without any error.?[/quote]

              Hi.
              You can pass socket id from main thread to child thread while creating of SerClass object class.inside thread class by creating QTcpsocket instance set this socket id to that instance.Remember, you can't use main socket directly to inside thread.
              otherwise you will face the error like the above.
              Hope this help.

              Be Cute

              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