错误:无法将(null)::readyRead()连接到mythread::readyRead()

Error: Cannot connect (null)::readyRead() to mythread::readyRead()

本文关键字:readyRead 连接 mythread 错误 null      更新时间:2023-10-16

我正在编写一个多线程服务器,但在"mythrad"类中编译时,我遇到了以下错误:1) 无法将(null)::readyRead()连接到mythread::readyRead()2) 无法将(null)::disconnected()连接到mythread::disconneged()我该怎么修?这是我的代码:

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);
}
显然,m_client就是nullptr
if (m_client) {
  connect(m_client, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
}

此外,您不需要从文档中明确断开连接:

所有来往对象的信号都会自动断开连接,并且该对象的任何挂起的已发布事件都会从事件队列中删除。但是,使用deleteLater()通常比直接删除QObject子类更安全。