我正在尝试使用QTCpSocket(telnet)从设备获取数据。 经过很多努力,我觉得我克服了一个问题,即Qtimer:starttimer无法从其他线程启动。
但是,现在我遇到了另一个使我感到困惑的问题。 我已经在互联网上浏览过一些帖子,但这并没有帮助我。 请检查我的代码,让我知道我的错误
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)
答案 0 :(得分:1)
创建worker
时,将在主线程中创建对象及其所有成员。然后,将worker
移动到其自己的线程,该线程将移动其所有子QObject。 worker
中的所有插槽现在都将在此新线程上执行。
从fduprocess
构造函数看来,您没有给套接字(_ClientSocketInstance
)赋予父项。这意味着套接字不会与工作线程移到同一线程,而是停留在主线程中,当您从插槽中使用它时会发出此警告。如果将this
传递给_ClientSocketInstance
构造函数使其成为worker
的子代,它将自动移至与worker
相同的线程,并且警告应消失。>