Hi,
I am using QWebChannel to expose easy communication interface to connected web browser clients.

Also I have overridden QWebChannelAbstractTransport interface which sends and receives data from/to QWebSocket.

When my client connects and all of the code runs in event loop of main thread all works great,
but when specific action occurs in the application I need to create new thread and move all of the client resources to this thread.
I have properly implemented parent->child hierarchy for moveToThread() operation.

When moveToThread() operation completes successfully and all object runs in new event loop (<- I think) clients can easily send me a message and I can easily process it in new thread,
but when my application trying to send message to client I am receiving console message error: "QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread".

I tried to debug it by printing threads in which objects or called methods works and it seems that everything is fine, but doesn't work.


TESTCASE
Part of my QWebChannelAbstractTransport implementation:

Output from qDebug line 11 looks like this: "Client in msg from thread >>> QThread(0x6aa270)"
Output from qDebug line 3 looks like this: "Client out msg from thread <<< QThread(0x6aa270) (socket: QThread(0x6aa270) )"
So it looks like all of object runs in the same thread but in line 6 I have a message: "QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread"
Of course socket is QWebSocket object.

Qt Code:
  1. void ClientTransportLayer::sendMessage(const QJsonObject &message)
  2. {
  3. qDebug() <<"Client out msg from thread <<< " <<QThread::currentThread() <<" (socket: " <<socket->thread() <<")";
  4.  
  5. QJsonDocument doc(message);
  6. socket->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
  7. }
  8.  
  9. void ClientTransportLayer::textMessageReceived(const QString &messageData)
  10. {
  11. qDebug() <<"Client in msg from thread >>> " <<QThread::currentThread();
  12.  
  13. QJsonParseError error;
  14. QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
  15.  
  16. if(!error.error && message.isObject())
  17. emit messageReceived(message.object(), this);
  18. }
To copy to clipboard, switch view to plain text mode