c++ - 错误:无法连接 (null)::readyRead() 到 mythread::readyRead()

标签 c++ multithreading qt

我正在编写一个多线程服务器,但在“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_clientnullptr

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/

相关文章:

c++ - 如何将 nullptr 作为参数传递?

c - 使用 Struct 将字符串数组和索引传递给 C 中的线程

c# - 如何正确关闭在线程中打开的 TcpClient

c++ - 当进程退出时收到通知

c++ - 命名空间级访问

c++ - 如何从文件系统获取 std::string 或 char[]?

c++ - 继承类的前向声明

c++ - 使用宽松的原子 bool 值来同步两个线程

c++ - QTextObjectInterface 与 Qml TextEdit (QQuickTextEdit)

multithreading - Qt + OpenCV 使用 std::thread 播放视频