我正在编写一个多线程服务器,但在“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(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);
}
最佳答案
显然,m_client
是 nullptr
。
if (m_client) {
connect(m_client, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
}
此外,您不需要从文档中明确断开连接:
All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. However, it is often safer to use deleteLater() rather than deleting a QObject subclass directly.
关于c++ - 错误:无法连接 (null)::readyRead() 到 mythread::readyRead(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177913/