Вопрос

Is it possible for a QTcpSocket to release its descriptor so ownership over the socket can be transferred to another QTcpSocket object on a different thread, or to a QSslSocket, without closing/interrupting the socket?

socket1.setDescriptor(descriptor);

// .....

descriptor = socket1.descriptor();
socket1.release();                       // <--- Is there a way to do this?
socket2.setSocketDescriptor(descriptor);
Это было полезно?

Решение

No, you can't release a socket descriptor once it has been assigned to a QTcpSocket.

But I'm not sure you would need to. The QTcpSocket can usually be moved to another thread with moveToThread, the constraints are the same as any moved QObject:

  • moveToThread should be called inside the thread to which the object currently belongs,
  • the object can't have a parent (you can remove it with theObject->setParent(0);, or move the parent instead of the object),

and because it is a QTcpSocket, the receiving thread should be running an event loop or you need to use the waitFor... functions in that thread.

This note seems to suggest that this only works for sockets since Qt 4.7.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top