c++qtqt5.9qtwebsockets

Why can't I connect QWebsocket::error SIGNAL to a lambda or any other SLOT type with identical signature? QT5.9


I'm trying to build a simple program that implements a QWebSocket, however, what I first tried to do is check if the connection is enabled. It didn't throw any exception since I never connected the error signal but I could check on my cloud server if any connection attempt had been made and saw that a connection never happened. So I tried to implement the error signal to see what the cause of the problem is, but I can't seem to make the signal connect to my printing function. I mean I always get a compile time error. If someone has any idea on how I should approach this, please say it!

I've tried like this also. I don't get any compile time errors however I get this runtime error:

QObject::connect(websocket, SIGNAL(error(QAbstractSocket::SocketError error)),cp, SLOT(OnWebSocketError(QAbstractSocket::SocketError error)));

Below are 3 images to illustrate the problem:

Main file: image 1

The error message: image 2

CustomPrinter class: image 3

Also ignote the "qDebug()<<""<error();" line it's commented now.

THis is the error I'm getting"

QObject::connect: No such signal QWebSocket::error(QAbstractSocket::SocketError error) in ../Websocket1/main.cpp:14"

Solution

  • When using the SIGNAL and SLOT macros you must not name the arguments, just their types.

    This is wrong:

    QObject::connect(websocket, SIGNAL(error(QAbstractSocket::SocketError error)),cp, SLOT(OnWebSocketError(QAbstractSocket::SocketError error)));
    

    This is correct:

    QObject::connect(websocket, SIGNAL(error(QAbstractSocket::SocketError)),cp, SLOT(OnWebSocketError(QAbstractSocket::SocketError)));
    

    Also it's best to not use the macros but the new syntax accepting function pointers, this way it does some compile time checks.

    QObject::connect(websocket, &QWebSocket::error, cp, &CustomPritner::OnWebSocketError);
    

    Remember that you must always use QObject* as arguments otherwise it won't compile.