我正在编写一个多线程服务器,但在“mythraed”类的编译期间,我收到以下错误“: 1)无法连接(null):: readyRead()到mythread :: readyRead() 2)无法连接(null):: disconnected()到mythread :: disconnected() 我该如何解决? 这是我的代码:
void mythread::run()
{
qDebug() << " Thread started";
connect(m_client, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(m_client, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << " Client connected";
exec();
}
void mythread::readyRead()
{
QByteArray Data = m_client->readAll();
qDebug()<< " Data in: " << Data;
m_client->write(Data);
}
void mythread::disconnected()
{
qDebug() << " Disconnected";
m_client->deleteLater();
exit(0);
}
myclient:
myclient::myclient(QObject* parent): QObject(parent)
{
QObject::connect(&m_client, SIGNAL(connected()),this, SLOT(startTransfer()));
}
void myclient::start(QString address, quint16 port)
{
QHostAddress LocalHost;
m_client.connectToHost(LocalHost, 6666);
}
void myclient::startTransfer()
{
m_client.write("Hello", 5);
}
答案 0 :(得分:1)
显然,m_client
是nullptr
。
if (m_client) {
connect(m_client, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
}
此外,您还不需要明确断开与文档的连接:
自动断开与对象之间的所有信号,并从事件队列中删除对象的所有待处理发布事件。但是,使用deleteLater()而不是直接删除QObject子类通常更安全。