为什么我不能为我的 QTcpServer 设置特定地址?

Why I can't set a specific address for my QTcpServer?

本文关键字:地址 设置 QTcpServer 不能 我的 为什么      更新时间:2023-10-16

我只想用一个特定的地址设置我的QTcpServer。我已经用这段代码试过了,但它不起作用...

  server.listen(QHostAddress::setAddress("127.0.0.1"),8888);

这是错误:

Cannot call member function 'bool QHostAddress::setAddress(const QString&)' without object 
server.listen(QHostAddress::setAddress("127.0.0.1"),8888);
                                                 ^

谁能帮我?

Cannot call member function 'bool QHostAddress::setAddress(const QString&)' without object

该错误告诉您 setAddress 不是一个静态方法,您必须在对象上调用它:

QHostAddress adr;
adr.setAddress("...");

在您的情况下,您可以只将 QHostAddress 构造函数与字符串参数一起使用:

server.listen(QHostAddress("127.0.0.1"),8888);