c++qtwebsocketqt5.3qtwebsockets

Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType()


I tried to put a QWebSocket connection into a QThread

QThread *thread = new QThread;



connect(&websocket,&QWebSocket::connected,this,&Widget::onWsConnect);

websocket.moveToThread(thread);
connect(thread, &QThread::finished, &websocket, &QObject::deleteLater);
websocket.open(wsUrl);

thread->start();

The program compiled without errors and is running normal, but when it tried to connect to the websocket server throws this error:

QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState' (Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)

When I do

websocket.open(wsUrl);

without threading the connection works fine.

Any ideas?


Solution

  • If you want to send your object through queued signal/slot connections, you should use qRegisterMetaType<T>(). Just call it before opening the socket some where in the constructor :

    qRegisterMetaType<QAbstractSocket::SocketState>();