Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt5 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5
1 Posts 1 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dl1ksv
    wrote on last edited by dl1ksv
    #1

    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 ?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved