QTcpSocket Disconnected() 未发出信号
Posted
技术标签:
【中文标题】QTcpSocket Disconnected() 未发出信号【英文标题】:QTcpSocket Disconencted() Signal not emitted 【发布时间】:2018-10-13 03:57:49 【问题描述】:我的代码似乎运行良好! (QObject:无法为位于不同线程中的父级创建子级:父级线程:QThread(0x221f650),当前线程:QThread (0x23a7950) : 此错误已解决
但是,当我拔掉 LAN 时,disconnected() 信号没有发出,因此 Reconenction timer 没有启动,因此我的代码停止! 我试图以这样一种方式制作我的代码,即使设备无法连接,** Reconenction timer ** 也会在每 10 秒 后启动,超时并连接到插槽 when_ReconnectionTimer_timeout() 一次又一次地尝试直到连接!但似乎断开的信号不起作用!
#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()<<"process thread"<< QThread::currentThreadId();
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........";
_ReconnectionTimerInstance.setSingleShot(true);
_ReconnectionTimerInstance.setInterval(10000);
_ReconnectionTimerInstance.start();
_iStatusPollTimer= startTimer(2000);
void fduprocess::when_ReconnectionTimer_timeout()
qDebug()<<"Reconnecting..";
// qDebug()<< _ClientSocketInstance.children();
_ClientSocketInstance.connectToHost(_strIPAddress, _usiPort);
qDebug()<<"tryin gto reconect..";
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() << "getting "<<get ;
这里是 fduprocess.h
#ifndef FDUPROCESS_H
#define FDUPROCESS_H
#include"fdu.h"
#include <QObject>
#include<QTimer>
#include<QTimerEvent>
#include<QTcpSocket>
class fduprocess : public QObject
Q_OBJECT
QString _strIPAddress;
quint16 _usiPort;
public:
explicit fduprocess(QObject *parent = 0);
QTcpSocket _ClientSocketInstance;
QTimer _ReconnectionTimerInstance ;
int _iStatusPollTimer;
void timerEvent(QTimerEvent *event);
signals:
public slots:
void tryit();
void doOntimeout();
void when_ReconnectionTimer_timeout();
void when_ClientSocketInstance_connected();
void when_ClientSocketInstance_disconnected();
void when_ClientSocketInstance_readyRead();
void when_ClientSocketInstance_error(QAbstractSocket::SocketError error);
;
#endif // FDUPROCESS_H
这里是 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........";
// worker->_ReconnectionTimerInstance = setSingleShot(true);
// worker->_ReconnectionTimerInstance.start(1000);
// worker->_iStatusPollTimer = startTimer(500);
worker->moveToThread(workerThread);
worker->_ClientSocketInstance.moveToThread(workerThread);
worker->_ReconnectionTimerInstance.moveToThread(workerThread);
connect(workerThread,SIGNAL(started()),worker,SLOT(tryit()));
connect(workerThread,SIGNAL(finished()),worker,SLOT(deleteLater()));
workerThread->start();
qDebug()<<"fdu thread "<<QThread::currentThreadId();
fdu::~fdu()
delete ui;
和main.cpp
#include "fdu.h"
#include <QApplication>
#include<QDebug>
int main(int argc, char *argv[])
QApplication a(argc, argv);
fdu w;
w.show();
qDebug()<<"main thrad" <<QThread::currentThreadId();
return a.exec();
任何人都可以在我犯错误的地方吗??
请忽略我的命名约定和缩进,因为我处于学习曲线中
【问题讨论】:
它不是重复的。那个问题已经解决了!这个问题是关于信号的。如果可能的话,请给一些建议 已经解决了,我建议你通过提供一个像样的minimal reproducible example来改进你的问题 我明白你的意思。我想我尽可能地做到了最低限度的完整性和可验证性。 你的代码完整吗代码,所以我建议专注于改进完整和可验证为minimal reproducible example 对不起!我已经添加了所需的标题。我希望现在一切都好。 【参考方案1】:解决了!看来我对一些概念感到困惑! 状态是Connecting,所以不能断开!
_ClientSocketInstance.disconnectFromHost();
_ReconnectionTimerInstance.start();
内部 when_ClientSocketInstance_error(QAbstractSocket::SocketError error)
这将实现我想要的。 抱歉,由于我的概念混乱而引起了所有人的注意。 感谢您的耐心等待!
【讨论】:
以上是关于QTcpSocket Disconnected() 未发出信号的主要内容,如果未能解决你的问题,请参考以下文章
BillingClient 始终返回 SERVICE_DISCONNECTED
Python QtNetwork.QTcpSocket.readAll() 和 QtNetwork.QTcpSocket.write() 不起作用