Qt5 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread



  • I just ported a working Qt4 Application from QT4 to Qt5.
    The structure in question is:

       class ProcessLogData : public QThread
       {
       Q_OBJECT
        public:
        ProcessLogData ( QObject *parent = 0 );
        ~ProcessLogData();
        void saveQsoData ( QString s );
        void run();
        void requestCallsign (QLabel **r, QString s );
        private:
        QTcpSocket *tcpSocket;
         ..... some other variables
          private slots:
           void doAction();
           void connectionClosedbyHost();
           void readAnswer();
          void setConnected();
          void setError ( QAbstractSocket::SocketError );
         signals:
           void unabletoConnect();
           void answerAvailable();
           void executeAction();
            };    
    

    The main code is:

     ProcessLogData::ProcessLogData ( QObject *parent )
    : QThread ( parent )   {
     tcpSocket = 0;
     connectionEstablished = false;
     connectionError = false;
     }
     void ProcessLogData::run() {
      connectionEstablished = false;
      connectionError = false;
      qRegisterMetaType<QAbstractSocket::SocketError> ( "QAbstractSocket::SocketError" );
      tcpSocket = new QTcpSocket();
      connect ( tcpSocket, SIGNAL ( disconnected() ), this, SLOT ( connectionClosedbyHost() ) , Qt::QueuedConnection);
      connect(this,SIGNAL(executeAction()),this,SLOT(doAction()),Qt::QueuedConnection);   
      connect ( tcpSocket, SIGNAL ( readyRead() ), this, SLOT ( readAnswer() ) ,Qt::QueuedConnection);
      connect ( tcpSocket, SIGNAL ( connected() ), this, SLOT ( setConnected() ) ,Qt::QueuedConnection);
     connect ( tcpSocket, SIGNAL ( error ( QAbstractSocket::SocketError ) ), this, SLOT ( setError ( QAbstractSocket::SocketError ) ) );
    tcpSocket->connectToHost ( QHostAddress::LocalHost, 8080, QIODevice::ReadWrite );
    tcpSocket->waitForConnected(6000);
    
    exec();
    }
    

    Data are transferred to the thread by calling:

    void ProcessLogData::requestCallsign( ... params ) {
       ...preparing actionString;
       emit executeAction();
     }
    

    Writing to the socket happens in doAction by

      int n = tcpSocket->write ( actionString.toLatin1(), actionString.length() );
      tcpSocket->flush();
    

    write and flush result in a message
    QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

    The data are transferred and the application works.
    But what am I doing wrong and how can I avoid the message ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.