解决QT开发中 qt.core.qobject.connect: QObject::connect: No such signal QTcpServer::QTcpServer::newConnect
🔥博客介绍`: 27dCnc
🎥系列专栏: <<数据结构与算法>> << 算法入门>> << C++项目>>
🎥 当前专栏: << QT开发>>
专题 : QT基础入门
👍👍👍👍👍👍👍👍👍👍👍👍
☆*: .。. o(≧▽≦)o .。.:*☆
❤️感谢大家点赞👍收藏⭐评论✍️
出现问题
问题如图: 无法检测信号:
原因
在 Qt5 中,newConnection()
信号是这样使用的:
connect(server, &QTcpServer::newConnection, this, &YourClass::onNewClientConnect);
而在 Qt6 中,你应该这样使用 hasNewConnection()
信号:
connect(server, &QTcpServer::hasNewConnection, this, &YourClass::onNewClientConnect);
确保槽函数 onNewClientConnect
是这样定义的:
private slots:
void onNewClientConnect() {
// 当有新的连接时,这个槽函数将被调用
QTcpSocket *socket = server->nextPendingConnection();
// 使用 socket 进行通信
}
};
如果你正在维护一个同时兼容 Qt5 和 Qt6 的代码库,你可能需要根据 Qt 版本来条件性地使用不同的信号。
解决方案
QTcpServer::newConnection()
信号是在 Qt5 中引入的,在 Qt6 中这个信号已经被移除了。在 Qt6 中,你应当使用 QTcpServer::hasNewConnection()
信号来代替。