c++ - Close QTcpSocket from another thread -
i use qtcpsocket
in dedicated thread. close thread, have personal slot connected disconnected
signal of qtcpsocket
. slot contains socket.close()
instruction.
class network { private: qtcpsocket *mp_socket; public: void connection() { mp_socket = new qtcpsocket(this); // move thread connect(mp_socket, signal(disconnected()), this, slot(disconnect())); } public slots: void disconnect() { mp_socket->close(); } };
here, slot works (even if socket in thread). if call disconnect slot myself, have following error :
"qsocketnotifier: socket notifiers cannot disabled thread".
why?
thanks help. ;)
ok, may need change design little. instead of moving socket new thread, make network
class object
derived, , move instead, , have socket parented it. previous answer revision did not work because close()
not registered qt meta system, because plain virtual function, not slot. new design can have interthread signals , slots solve problem. connections between objects in different threads automatically resolved queued , use event loop schedule execution in correct thread.
also, notice have slot in current class, without being qobject
derived. not work, need qt's meta system have slots. don't error, because slots
compiles nothing in c++, used moc (meta object compiler), don't have slot.
naturally, means qmetaobject::invokemethod(yournetworkclass, "disconnect", qt::queuedconnection)
work.
Comments
Post a Comment