[Solved] UI events blocked the QLocalSocket 'readyRead' signal\slot
- I'm using the QLocalSocket as an IPC to pass data from two applications (for the discussion: from writeApp to readApp).
- writeApp write to the socket at ~10Hz (every 100 msec)
- readApp read from the socket when readyRead emmited
- readApp have a gui.
Some code (The readApp is implemented with QThread, but it's not necessary, as explained below):
class writeApp: public QObject
writeApp(QString servername, QObject *parent = 0);
void onDataReady(QByteArray data);@
@writeApp::writeApp(QString servername, QObject *parent)
m_socket = new QLocalSocket(this);
void writeApp::onDataReady(QByteArray data)
class readApp: public QThread
@readApp::readApp(QString servernameCanA, QString servernameCanB)
m_server = new QLocalServer(this);
qDebug() << "Not able to start the Server";
connect(m_server, SIGNAL(newConnection()), this, SLOT(socket_new_connection()));
clientConnection = new QLocalSocket(this);
clientConnection = m_server->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater())); connect(clientConnection, SIGNAL(readyRead()), this, SLOT(onSocketReadReady()));
QByteArray m_data = clientConnection->read(clientConnection->bytesAvailable());
When i'm drag the window of the readApp, the readReady signal (or maybe the slot?) seem to be blocked (the program do not enter to the onSocketReadReady). Maybe by the windows events?
(I'm tried to seperate the readApp to two threads, such that the socket reading will have another eventLoop from the GUI - but nothing seem to be changed...)
Thanks for advance,
Your readApp implementation is a subclass of QThread and only the stuff that is done in the run() function is executed in another thread. Since the slots socket_new_Connection() and onSocketReadReady() belong to the QThread subclass readApp (which will live and work in the context of the thread were it was created) they are also running in the context of the main thread.
The whole subclassing of QThread approach is not really recommended for your use case. Try to implement it using the "worker object aproach":http://qt-project.org/wiki/QThreads_general_usage.
Edit: I just saw you did the old moveToThread(this) trick in the subclasses constructor. Have you already read the "You're doing it wrong":http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ article?
Yhea! Its work!