QObject:无法为处于不同线程中的父对象创建子对象:父对象的 thread:QThread(0x221f650),当前的 thread:QThread(0x23a7950)

QObject: Cannot create children for a parent that is in a different thread: parent's thread:QThread(0x221f650), current thread:QThread(0x23a7950)

我正在尝试使用 QTCpSocket (telnet) 从设备获取数据。 经过一番努力后,我觉得我克服了一个 Qtimer:starttimer 无法从其他线程启动的问题。

但是,现在我遇到了另一个让我很困惑的问题。 我已经在互联网上浏览了一些 post,但它对我没有帮助。 请检查我的代码并让我知道我的错误

fdu.cpp

#include "fdu.h"
#include "ui_fdu.h"
#include"fduprocess.h"
fdu::fdu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::fdu)
{
    ui->setupUi(this);
    QThread *workerThread =  new QThread;
    fduprocess *worker = new fduprocess;

 qDebug()<< "...goind to fduprocess........";
    workerThread->start();
    worker->_ReconnectionTimerInstance.setSingleShot(true);
    worker->_ReconnectionTimerInstance.start(1000);
    worker->_iStatusPollTimer = startTimer(500);
    worker->moveToThread(workerThread);
    connect(workerThread,SIGNAL(started()),worker,SLOT(tryit()));
    connect(workerThread,SIGNAL(finished()),worker,SLOT(deleteLater()));

}

fdu::~fdu()
{
    delete ui;
}

fduprocess.cpp

#include "fduprocess.h"
#include<QDebug>
#include<QThread>
fduprocess::fduprocess(QObject *parent) : QObject(parent)
{

}

void fduprocess::timerEvent(QTimerEvent *event)
{
    if(event->timerId()== _iStatusPollTimer)
    {

        if(_ClientSocketInstance.state() == QAbstractSocket::ConnectedState)
        {
            _ClientSocketInstance.write("alarmstat\r\n"); //25
             _ClientSocketInstance.write("selectedin\r\n"); // 4
              _ClientSocketInstance.write("sigoutstat\r\n"); //13
           _ClientSocketInstance.write("disablestat\r\n"); //5

           _ClientSocketInstance.write("pwrstat\r\n"); //5

           _ClientSocketInstance.write("siginstat\r\n");//5
        }
    }
}

void fduprocess::tryit()
{
 qDebug()<< "...came inside tryit to tryit.........";
 connect(&_ReconnectionTimerInstance,SIGNAL(timeout()), this, SLOT(when_ReconnectionTimer_timeout()));
  qDebug()<< "...conencted to whentimeout.........";
 connect(&_ClientSocketInstance,SIGNAL(connected()), this, SLOT(when_ClientSocketInstance_connected()));
 connect(&_ClientSocketInstance,SIGNAL(disconnected()), this, SLOT(when_ClientSocketInstance_disconnected()));
 connect(&_ClientSocketInstance,SIGNAL(readyRead()), this, SLOT(when_ClientSocketInstance_readyRead()));
 connect(&_ClientSocketInstance,SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(when_ClientSocketInstance_error(QAbstractSocket::SocketError)));
 _strIPAddress = "192.168.1.135";
 _usiPort = 23;
  qDebug()<< "...end tryit........";


}

void fduprocess::doOntimeout()
{
    qDebug()<< "................";
}

void fduprocess::when_ReconnectionTimer_timeout()
{
    qDebug()<<"Reconnecting..";
      // qDebug()<< _ClientSocketInstance.children();
        _ClientSocketInstance.connectToHost(_strIPAddress, _usiPort);
}

void fduprocess::when_ClientSocketInstance_connected()
{ qDebug()<<"Connected......";

    _ClientSocketInstance.write("endrun_1");
    _ClientSocketInstance.flush();
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.flush();

}

void fduprocess::when_ClientSocketInstance_disconnected()
{
    qDebug()<<"Disconnected";
        _ReconnectionTimerInstance.start();
}

void fduprocess::when_ClientSocketInstance_readyRead()
{
    QByteArray get = _ClientSocketInstance.readLine();
    qDebug() << get ;
}

void fduprocess::when_ClientSocketInstance_error(QAbstractSocket::SocketError error)
{

}

非常欢迎关于任何其他代码味道的任何其他反馈 请忽略我的缩进和命名约定,因为我处于学习曲线中。 这是控制台输出:

...goind to fduprocess........
...came inside tryit to tryit.........
...conencted to whentimeout.........
...end tryit........
Reconnecting..
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x609250), parent's thread is QThread(0x221f650), current thread is QThread(0x23a7950)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x609250), parent's thread is QThread(0x221f650), current thread is QThread(0x23a7950)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x609250), parent's thread is QThread(0x221f650), current thread is QThread(0x23a7950)

创建worker时,对象及其所有成员都在主线程中创建。然后,将 worker 移动到它自己的线程,这将移动它的所有子 QObject。 worker 中的所有插槽现在都将在此新线程上执行。

fduprocess 构造函数看来,您似乎没有为您的套接字提供父级 (_ClientSocketInstance)。这意味着套接字不会移动到与工作线程相同的线程,而是保留在主线程中,当您从插槽中使用它时会发出此警告。如果将 this 传递给 _ClientSocketInstance 构造函数以使其成为 worker 的子级,它将自动移动到与 worker 相同的线程并且警告应该消失。