我正在编写一个简单的EchoServer
类,它继承自QTcpServer
。当我在构造函数中连接信号和插槽时,它不会很好。在派生的QObject构造函数中连接Qt信号和插槽
class EchoServer : public QTcpServer {
//Q_OBJECT
public:
EchoServer(int listenling_port) {
this->listen(QHostAddress(), listenling_port);
connect(this, SIGNAL(newConnection()), this, SLOT(HandleIncomingConnection()));
}
public slots:
void HandleIncomingConnection() {
auto echo_handler = EchoServerHandler(this->nextPendingConnection());
echo_handler.Echo();
}
private:
};
应用程序确实监听端口,并且可以通过telneted。但控制台显示
“的QObject ::连接:没有这样的槽QTcpServer既可:: HandleIncomingConnection()”,这似乎它被识别this
作为基类QTcpServer
指针,
。
另外,如果我在代码中留下Q_OBJECT
,它不会编译,说
“错误LNK2001:无法解析的外部符号‘市民:虚拟结构QMetaObject常量* __thiscall的EchoServer ::元对象(无效)常量’( ?metaObject @ EchoServer @@ UBEPBUQMetaObject @@ XZ)“,
它们是相关的吗?
可能重复[Q \ _OBJECT链接器错误!](http://stackoverflow.com/questions/3264259/q-object-linker-error) – sashoalm 2013-04-25 06:57:29
@sashoalm尽管链接有帮助。该链接中的答案不能解决问题,因此不是重复的。 – WiSaGaN 2013-04-25 07:06:59