标题可能难以理解,抱歉。
我的插槽连接信号有问题。没有编译错误,但连接返回 false。控制台中打印了错误行:
QObject::connect: No such slot QObject::clientConnected_() in ../server/server.cpp:8
我认为问题在于连接在类 Server 中看不到插槽 clientConnected_()。或者可能出于某种原因在类 QObject 中寻找它。
代码如下:
服务器.h
#ifndef SERVER_H
#define SERVER_H
#include <QtNetwork>
#include <QTcpServer>
#include <QObject>
class Server : public QObject
{
Q_OBJECT
public:
bool startListening(const quint16 port);
public slots:
static void clientConnected_();
private:
QTcpServer * server_;
};
#endif // SERVER_H
服务器.cpp
#include "server.h"
#include <iostream>
bool Server::startListening(const quint16 port)
{
server_ = new QTcpServer();
QObject::connect(server_,SIGNAL(newConnection()),this,SLOT(clientConnected_()));
return server_->listen(QHostAddress::Any,port);
}
void Server::clientConnected_()
{
std::cout << "Connection established!" << std::endl;
return;
}
有任何想法吗?