0

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?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Reza
  • 3,473
  • 4
  • 35
  • 54

1 Answers1

2

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());
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    Dear @Marker R, connecting in that way requires both tcpSocket and tcpServer to be a simple pointer not a Qpointer. so it does not work in this case. – Reza Jun 09 '15 at 08:15
  • @thung Thanks for your comment. It works for me. First please write it as an answer to this question for others. Second qt official help says that: "A guarded pointer, QPointer, behaves like a normal C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases).", and I have a lot of signal/slot connection which did not use the QPointer::data for them and they are working fine (At least I think :-|). So should I correct them too? – Reza Jun 09 '15 at 12:26
  • @Reza Are you sure you didn't use the old connect syntax for the working connections? For example `connect(object, SIGNAL(objectSignal()), object, SLOT(objectSlot()));` – thuga Jun 09 '15 at 12:37
  • @thuga Yes, I'm using the old connect syntax 'connect(object, SIGNAL(objectSignal()), object, SLOT(objectSlot()));'. Do you mean Qpointer works with this kind of connecting signal/slot? – Reza Jun 09 '15 at 12:59
  • @Reza It appears so, but it doesn't make any sense to me. – thuga Jun 09 '15 at 13:00