c++qttcpqtcpsocketqtcpserver

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


I just want to setup my QTcpServer with a specific address. I have tried it with this code but it does not work...

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

This is the Error:

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

Can anyone help me?


Solution

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

    That error tells you that setAddress is not a static method, you have to call it on an object:

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

    In your case you can just use the QHostAddress constructor with a string parameter:

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