Using QPointer and QObject::connect with C++11

864 views Asked by At

I'm using Qt 5.4 and trying to resume accepting new connections when the "disconnected" signal from my QTcpSocket is emitted. So I wrote the following in .h and .cpp file respectively:

QPointer<QTcpServer> tcpServer; // in .h file

connect(tcpSocket, &QAbstractSocket::disconnected, [=](){
        tcpServer->resumeAccepting(); // in .cpp file
    });

As you can see, I use QPointer class for tcpSocket. With the above codes, I'm not able to build my program and receive "no matching function for call to ..." error while I don't have any problem in case of defining tcpSocket without QPointer.

How should I solve this problem?

1

There are 1 answers

5
Marek R On BEST ANSWER

It would be smarter to do direct connection.

 connect(tcpSocket, &QAbstractSocket::disconnected, 
         tcpServer, &QTcpServer::resumeAccepting);

Please note that nextPendingConnection has parent set to QTcpServer, so you can also access server by:

 auto tcpServer = qobject_cast<QTcpServer *>(sender()->parent());