Help needed for using QTcpSocket in a QThread



  • 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());
    }
    @


  • Lifetime Qt Champion

    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.



  • 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.?


  • Lifetime Qt Champion

    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.



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



  • [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.


Log in to reply
 

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