QThread problem for object in QT
I'm trying to create a class will do some communication between my client and my server. I'd like it to have it's own thread.
My code is
[CODE]
This didn't work. It didn't work, because the code for TcpMysocket wasn't executed in the same thread the parent object currently is. So what I decided to do is to create slot and signal for setup and emit it in my constructor. Here is how my code looks right now.Code:class Sample : public QObject { Q_OBJECTpublic: Sample(); ~Sample();private: QThread *my_thread; QTcpsocket *Mysock;}Here is how I coded my constructor: Sample::Sample(){ my_thread = new QThread(); my_thread -> start(); moveToThread(my_thread); Mysock = new QTcpsocket(this); Mysock -> connectToHost("host", port);}
void Sample::setup(){Code:class Sample : public QObject { Q_OBJECTpublic: Sample(); ~Sample();public slots: void setup();signals: void do_setup();private: QThread *my_thread; QTcpsocket *Mysock;}And some of it's implementation Sample::Sample(){ my_thread = new QThread(); my_thread -> start(); moveToThread(my_thread); connect(this, SIGNAL(do_setup()), this, SLOT(setup())); emit do_setup(); }
Mysock = new QTcpsocket();
Mysock -> connectToHost("host", port);
}
how to delete QThread smoothly and all the class objects..
Any Advice
Bookmarks