zyangxue
10th December 2009, 07:49
All,
In below codes, I get a QObject warning message during running:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0x9cb3f98), parent's thread is BackServer(0x9ca2640), current thread is QThread(0x9c46378)
class BackServer : QThread{
public:
BackServer(QHostAddress ip, quint16 port, QObject * parent=0);
......
public slots:
void sendMsg(QString);
protected:
void run();
private slots:
void slot_newConn();
......
private:
QTcpServer * m_ser;
QTcpSocket * m_conn;
QHostAddress m_ip;
quint16 m_port;
};
void BackServer::run(){
m_ser=new QTcpServer;
connect(m_ser, SIGNAL(newConnection()), SLOT(slot_newConn()), Qt::DirectionConnect);
m_ser->listen(m_ip, m_port);
exec();
}
void BackServer::slot_conn(){
m_conn=m_ser->nextPendingConnection();
}
void BackServer::sendMsg(QString msg){
if (m_conn){
m_conn->write(msg.toUtf8()); // < -- issue occured point
m_conn->waitForBytesWritten();
}
}
I use BackServer as a member of subclass of QMainWindow:
class MainWindow{
...
private slots:
void slot_sendMsg();
void slot_startServer();
signal:
void sendMsg(QString);
private:
BackServer * m_backServer;
};
class MainWindow::MainWindow(...){
...
connect(ui->sendButton, SIGNAL(clicked()), SLOT(slot_sendMsg()));
...
}
class MainWindow::slot_startServer(){
...
m_backServer=new BackServer(ip, port);
connect(this, SIGNAL(sendMsg(QString), m_backServer, SLOT(sendMsg(QString);
m_backServer->start();
...
}
//
// whichever, the QObject create child thread occured!
//
class MainWindow::slot_sendMsg(){
#if 0
if (m_backServer && m_backServer->isRunning()){
m_backServer->sendMsg(ui->lineEdit->text());
}
#else
emit sendMsg(ui->lineEdit->text());
#endif
}
any suggestion?
attach is my Qtcode.
In below codes, I get a QObject warning message during running:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0x9cb3f98), parent's thread is BackServer(0x9ca2640), current thread is QThread(0x9c46378)
class BackServer : QThread{
public:
BackServer(QHostAddress ip, quint16 port, QObject * parent=0);
......
public slots:
void sendMsg(QString);
protected:
void run();
private slots:
void slot_newConn();
......
private:
QTcpServer * m_ser;
QTcpSocket * m_conn;
QHostAddress m_ip;
quint16 m_port;
};
void BackServer::run(){
m_ser=new QTcpServer;
connect(m_ser, SIGNAL(newConnection()), SLOT(slot_newConn()), Qt::DirectionConnect);
m_ser->listen(m_ip, m_port);
exec();
}
void BackServer::slot_conn(){
m_conn=m_ser->nextPendingConnection();
}
void BackServer::sendMsg(QString msg){
if (m_conn){
m_conn->write(msg.toUtf8()); // < -- issue occured point
m_conn->waitForBytesWritten();
}
}
I use BackServer as a member of subclass of QMainWindow:
class MainWindow{
...
private slots:
void slot_sendMsg();
void slot_startServer();
signal:
void sendMsg(QString);
private:
BackServer * m_backServer;
};
class MainWindow::MainWindow(...){
...
connect(ui->sendButton, SIGNAL(clicked()), SLOT(slot_sendMsg()));
...
}
class MainWindow::slot_startServer(){
...
m_backServer=new BackServer(ip, port);
connect(this, SIGNAL(sendMsg(QString), m_backServer, SLOT(sendMsg(QString);
m_backServer->start();
...
}
//
// whichever, the QObject create child thread occured!
//
class MainWindow::slot_sendMsg(){
#if 0
if (m_backServer && m_backServer->isRunning()){
m_backServer->sendMsg(ui->lineEdit->text());
}
#else
emit sendMsg(ui->lineEdit->text());
#endif
}
any suggestion?
attach is my Qtcode.