connecting a signal from a QTcpSocket isn't working
-
I'm learning how to use QTcpSockets and servers so I'm setting up my test classes to play around with. However on my "client" app, I'm trying to connect the signal "connected" from the socket to a timer. I get the error
error: no matching function for call to ‘SocketTestApp::connect(QTcpSocket*&, void (QAbstractSocket::*)(), QTimer*&, <unresolved overloaded function type>)’ QObject::connect(_socket, &QTcpSocket::connected, _timer, &QTimer::start); ^
But that doesn't make any sense, see the below code. The first QObject::connect has no problems, but the second one gives that error. I added the QObject in hopes it would fix it but it didn't. What is going on here? It looks like it thinks the second connect statement is calling QTcpSocket::connect, but I'm explicitly telling it to use QObject::connect.
SocketTestApp::SocketTestApp(QWidget *parent) : QMainWindow(parent), ui(new Ui::SocketTestApp), _socket(new QTcpSocket(this)), _timer(new QTimer(this)) { ui->setupUi(this); QObject::connect(_timer, &QTimer::timeout, this, &SocketTestApp::transmitMessage); QObject::connect(_socket, &QTcpSocket::connected, _timer, &QTimer::start); _timer->setInterval(3000); }
-
It is becz overloaded start() methods in QTimer.
Try to using - QObject::connect(_socket, SIGNAL(connected()), _timer,SLOT( start());
-
Yup, that fixed it. Please explain how you determined that from the error. As my reading of the error was wrong.
-
Just look at the Qt Creator error. It should have given you more error than this. It must be explicit saying multiple overloaded methods. I don't know which one.
-
@graniteDev Explanation: QTimer has two start() methods: one without parameters and one with a parameter. If you do it like
connect(_socket, &QTcpSocket::connected, _timer, &QTimer::start);
the compiler doesn't know which one you mean.
That's why it says "<unresolved overloaded function type>"
You can see here what you can do in such a situation: https://riptutorial.com/qt/example/17048/connecting-overloaded-signals-slotsconnect(_socket, &QTcpSocket::connected, _timer, qOverload<>(&QTimer::start));
For qOverload you need to make sure your compiler supports C++14 and that it is activated.
If you only have C++11 you can doconnect(_socket, &QTcpSocket::connected, _timer, QOverload<>::of((&QTimer::start));