Hello every one,
I am really grateful for such a wonderful support forum for QT. I could get most of my question answered till today except this one... Great going...
the basic functionality:
create a gui application and then create a thread that holds a tcpsocket connection and perform a tedious calculation on the data received on the Socket..
the tcp connection at which the data is received is LocalHost:3000
I created a basic QT GUI application and then at the press of a button; i would create a thread with the command...
mythread *txt = new mythread();
txt->start();
mythread *txt = new mythread();
txt->start();
To copy to clipboard, switch view to plain text mode
the thread is defined with the following constructor...
mythread.h
{
Q_OBJECT
public:
void run();
private :
}
class mythread : public QThread
{
Q_OBJECT
public:
mythread(QObject *parent = 0);
void run();
private :
QTcpSocket socket;
}
To copy to clipboard, switch view to plain text mode
mythread.cpp
mythread
::mythread(QObject *parent
){
}
void mythread::run(){
qint64 maxlen = 128;
if (socket.waitForConnected(1000))
qDebug("Connected!");
char buffer[128];
//QDataStream in(&socket);
//in.setVersion(QDataStream::Qt_4_6);
while(1){
qint64 len = socket.readLine(buffer, maxlen);
if(len < 0){
qDebug()<<"the break function";
break;
}
qDebug()<<"The buffer is" <<buffer<<"and its size is "<<len;
}
}
mythread::mythread(QObject *parent)
: QThread(parent)
{
}
void mythread::run(){
qint64 maxlen = 128;
socket.connectToHost(QHostAddress::LocalHost,3000);
if (socket.waitForConnected(1000))
qDebug("Connected!");
char buffer[128];
//QDataStream in(&socket);
//in.setVersion(QDataStream::Qt_4_6);
while(1){
qint64 len = socket.readLine(buffer, maxlen);
if(len < 0){
qDebug()<<"the break function";
break;
}
qDebug()<<"The buffer is" <<buffer<<"and its size is "<<len;
}
}
To copy to clipboard, switch view to plain text mode
After i run this, i always get the error saying that
QObject: Cannot create children for a parent that is in a different thread. (Parent is QTcpSocket(0x980df60), parent's thread is QThread(0x3d4ec8), current thread is mythread(0x980df58)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x980df60), parent's thread is QThread(0x3d4ec8), current thread is mythread(0x980df58)
Connected!
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
The buffer is and its size is 0
Could any one help me in this regard...
Thanking you
Rajesh Medampudi....
Bookmarks