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. Console application & QTcpSocket
Qt 6.11 is out! See what's new in the release blog

Console application & QTcpSocket

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 5 Posters 15.3k Views 1 Watching
  • 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.
  • B Bonnie

    I don't understand why do you replace a.exec() with your own loop?
    QApplication is not a console application by the way...

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #6

    @Bonnie , because originally I had a thread to do everything in the background I wanted to do, but then I thought why don't I just have my loop function and put the thread functionality in that...calling the standard qt loop means I need the thread back to do everything else like sending messages.

    1 Reply Last reply
    0
    • SPlattenS SPlatten

      @Bonnie , tell my application that, I call it and its working (in a fashion), are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @SPlatten said in Console application & QTcpSocket:

      are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

      What is not working with QCoreApplication?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @Bonnie , tell my application that, I call it and its working (in a fashion), are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

        B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #8

        @SPlatten
        QApplication inherits QGuiApplication.
        So what you have is a gui application but without widgets.

        We don't know much about your application so we can't know why it doesn't work.
        Maybe you can provide a minimum example?

        I've tested with a little code snippet because of your previous post.
        This socket can connect and write data.

        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QTcpSocket socket;
        
            QObject::connect(&socket, &QTcpSocket::bytesWritten, [](qint64 bytes){
                qDebug() << bytes << "written";
            });
        
            QObject::connect(&socket, &QTcpSocket::connected, [&](){
                QDataStream stream;
                qDebug() << "connected";
                stream.setDevice(&socket);
                stream.setVersion(QDataStream::Qt_5_14);
                stream << "TEST";
            });
        
            qDebug() << "connecting";
            socket.connectToHost("localhost", 8123);
        
            return a.exec();
        }
        
        1 Reply Last reply
        4
        • jsulmJ jsulm

          @SPlatten said in Console application & QTcpSocket:

          are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

          What is not working with QCoreApplication?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #9

          @jsulm , I've just installed the Debug Symbols and I still can't step into QIODevice/write to see what's happening.

          The console is connected to the server, but it doesn't seem to be writing data.

          1 Reply Last reply
          0
          • SPlattenS SPlatten

            @Bonnie , tell my application that, I call it and its working (in a fashion), are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #10

            @SPlatten said in Console application & QTcpSocket:

            Tell my application that, I call it and its working (in a fashion), are they're any checks I could perform that would indicate what I need to do to get QTcpSocket working?

            I am sorry to write it but you are doing the hole thing wrong :(
            It is certainly because you have a leak of knowledge about how Qt works.
            Qt is an strongly asynchronous based framework.
            Please take time to acquire this knowledge, there are basic things to know:

            • https://doc.qt.io/qt-5/threads-qobject.html
            • https://www.kdab.com/wp-content/uploads/stories/multithreading-with-qt-1.pdf

            There are many ways to make work an application, but not all are recommended.

            Some basic thing to take care:

            • avoid to lock the used thread, this will "kill" signals/slots handling by the event loop
            • avoid processEvents() usage, there may be case where you need to do it, but in general this is a good indicator for bad implementation.

            If you want to wait an signal to be emitted, you should use a local QEventLoop

            QEventLoop myLoop;
            
            // exit loop after 30 seconds with exit code -1 (timeout)
            QTimer::singleShot(30000, &myLoop, [&myLoop]() { myLoop.exit(-1); });
            // signal which will stop wait
            connect(&myObject, &WorkingClass::done, &myLoop, &QEventLoop::quit);
            
            int result = myLoop.exec(); // wait until signal is emitted or timeout
            if(result < 0)
                qDebug() << "Timeout!!";
            
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #11

              @Bonnie , @KroMignon , in my console application what I want to achieve is the following:

              • At a fixed frequency send a heartbeat message to a QTcpServer
              • Call an derived body function do perform any other required activities

              The above is currently done in my loopUntilExit function, but the message isn't being written and I never get a bytesWritten signal raised.

              jsulmJ KroMignonK 2 Replies Last reply
              0
              • SPlattenS SPlatten

                @Bonnie , @KroMignon , in my console application what I want to achieve is the following:

                • At a fixed frequency send a heartbeat message to a QTcpServer
                • Call an derived body function do perform any other required activities

                The above is currently done in my loopUntilExit function, but the message isn't being written and I never get a bytesWritten signal raised.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @SPlatten said in Console application & QTcpSocket:

                At a fixed frequency send a heartbeat message to a QTcpServer

                Qtimer

                "Call an derived body function do perform any other required activities" - call it, or is there problems doing so?

                "I've just installed the Debug Symbols and I still can't step into QIODevice/write to see what's happening." - isn't this from your other thread and not relevant for this one? Please don't mix topics. To step into Qt code you have to install Qt source code and set the path to it in QtCreator. Debug symbols only help to get meaningful stack traces.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @Bonnie , @KroMignon , in my console application what I want to achieve is the following:

                  • At a fixed frequency send a heartbeat message to a QTcpServer
                  • Call an derived body function do perform any other required activities

                  The above is currently done in my loopUntilExit function, but the message isn't being written and I never get a bytesWritten signal raised.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #13

                  @SPlatten said in Console application & QTcpSocket:

                  in my console application what I want to achieve is the following:

                  • At a fixed frequency send a heartbeat message to a QTcpServer
                  • Call an derived body function do perform any other required activities

                  To complete the application done by @Bonnie :

                  int main(int argc, char *argv[])
                  {
                      QCoreApplication a(argc, argv);
                  
                      QTcpSocket socket;
                      QTimer heartBeat;
                  
                      heartBeat.setSingleShot(false);
                      heartBeat.setInterval(5000); // 5 seconds
                      QObject::connect(heartBeat, &QTimer::timeout, [&](){
                          QDataStream stream;
                          qDebug() << "heartBeat";
                          stream.setDevice(&socket);
                          stream.setVersion(QDataStream::Qt_5_14);
                          stream << "heartBeat";
                      };
                      
                      QObject::connect(&socket, &QTcpSocket::bytesWritten, [&](qint64 bytes){
                          qDebug() << bytes << "written";
                      });
                  
                      QObject::connect(&socket, &QTcpSocket::connected, [&](){
                          QDataStream stream;
                          qDebug() << "connected";
                          stream.setDevice(&socket);
                          stream.setVersion(QDataStream::Qt_5_14);
                          stream << "TEST";
                          heartBeat.start();
                      });
                      
                      QObject::connect(&socket, &QTcpSocket::disconnected, [&]() {
                           qDebug() << "Bye bye";
                          a.exit();
                      };
                      
                      qDebug() << "connecting";
                      socket.connectToHost("localhost", 8123);
                  
                      return a.exec();
                  }
                  
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  3
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #14

                    @Bonnie ,@jsulm ,@KroMignon , odd results now, I've modified the console app main to:

                    QCoreApplication a(intArgc, parystrArgv);
                    QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                    clsModFileIO obj(&a, intArgc, parystrArgv);
                    return a.exec();
                    

                    In the clsModFileIO constructor:

                    QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                    QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
                    QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
                    QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                    QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
                    QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
                    QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
                    

                    This class is derived from QTcpSocket. OnConnected is a pure virtual slot. I am getting a receipt in onDataIn, but the encoding looks messed up, I am sending on JSON in my messages, what I get back looks like Japanese:

                    笢浯摵汥∺≘䵌䵐䅍≽
                    
                    jsulmJ KroMignonK B 3 Replies Last reply
                    0
                    • SPlattenS SPlatten

                      @Bonnie ,@jsulm ,@KroMignon , odd results now, I've modified the console app main to:

                      QCoreApplication a(intArgc, parystrArgv);
                      QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                      clsModFileIO obj(&a, intArgc, parystrArgv);
                      return a.exec();
                      

                      In the clsModFileIO constructor:

                      QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                      QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
                      QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
                      QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                      QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
                      QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
                      QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
                      

                      This class is derived from QTcpSocket. OnConnected is a pure virtual slot. I am getting a receipt in onDataIn, but the encoding looks messed up, I am sending on JSON in my messages, what I get back looks like Japanese:

                      笢浯摵汥∺≘䵌䵐䅍≽
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @SPlatten said in Console application & QTcpSocket:

                      what I get back looks like Japanese

                      Can't comment on that as I don't know what you are sending back and what encoding you're using.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      SPlattenS 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @SPlatten said in Console application & QTcpSocket:

                        what I get back looks like Japanese

                        Can't comment on that as I don't know what you are sending back and what encoding you're using.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #16

                        @jsulm, I haven't called any encoding methods, what is the default?

                        jsulmJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @jsulm, I haven't called any encoding methods, what is the default?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          @SPlatten For QString you can find this information in the documentation: https://doc.qt.io/qt-5/qstring.html

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @Bonnie ,@jsulm ,@KroMignon , odd results now, I've modified the console app main to:

                            QCoreApplication a(intArgc, parystrArgv);
                            QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                            clsModFileIO obj(&a, intArgc, parystrArgv);
                            return a.exec();
                            

                            In the clsModFileIO constructor:

                            QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                            QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
                            QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
                            QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                            QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
                            QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
                            QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
                            

                            This class is derived from QTcpSocket. OnConnected is a pure virtual slot. I am getting a receipt in onDataIn, but the encoding looks messed up, I am sending on JSON in my messages, what I get back looks like Japanese:

                            笢浯摵汥∺≘䵌䵐䅍≽
                            
                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by KroMignon
                            #18

                            @SPlatten said in Console application & QTcpSocket:

                            This class is derived from QTcpSocket. OnConnected is a pure virtual slot. I am getting a receipt in onDataIn, but the encoding looks messed up, I am sending on JSON in my messages, what I get back looks like Japanese:
                            笢浯摵汥∺≘䵌䵐䅍≽

                            Again this depends how you have written data on socket and how you read them on the other side.
                            You should know that for Qt QString internally always stored in UTF-8, but when you send it "outside" you have to take care about which string format is to be used.

                            Please show the JSON write to socket and JSON read from socket code.

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @Bonnie ,@jsulm ,@KroMignon , odd results now, I've modified the console app main to:

                              QCoreApplication a(intArgc, parystrArgv);
                              QCoreApplication::setApplicationName(clsModFileIO::scpszTitle());
                              clsModFileIO obj(&a, intArgc, parystrArgv);
                              return a.exec();
                              

                              In the clsModFileIO constructor:

                              QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
                              QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
                              QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
                              QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
                              QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
                              QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
                              QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
                              

                              This class is derived from QTcpSocket. OnConnected is a pure virtual slot. I am getting a receipt in onDataIn, but the encoding looks messed up, I am sending on JSON in my messages, what I get back looks like Japanese:

                              笢浯摵汥∺≘䵌䵐䅍≽
                              
                              B Offline
                              B Offline
                              Bonnie
                              wrote on last edited by Bonnie
                              #19

                              @SPlatten
                              I remember you're sending a QByteArray ( from QJsonDocument::toJson() ) by QDataStream.
                              Do you also use QDataStream to read to a QByteArray in onDataIn?

                              SPlattenS 1 Reply Last reply
                              1
                              • B Bonnie

                                @SPlatten
                                I remember you're sending a QByteArray ( from QJsonDocument::toJson() ) by QDataStream.
                                Do you also use QDataStream to read to a QByteArray in onDataIn?

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by SPlatten
                                #20

                                @Bonnie , @jsulm , @KroMignon , this is my send function:

                                void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                    if ( isOpen() != true ) {
                                        return;
                                    }
                                    //Associate this TCP socket with the output data stream
                                    QByteArray arybytMsg;
                                    QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                                    //dsOut.setDevice(this);
                                    dsOut.setVersion(clsJSON::mscintQtVersion);
                                    //Send message to data stream
                                    dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                    //Write message
                                    write(arybytMsg);
                                }
                                

                                And receiving:

                                void clsModFileIO::onDataIn() {
                                    QDataStream dsIn;
                                    dsIn.setDevice(this);
                                    dsIn.setVersion(clsJSON::mscintQtVersion);
                                
                                    QString strRx;
                                    dsIn.startTransaction();
                                    dsIn >> strRx;
                                
                                    if ( strRx.isEmpty() != true ) {
                                        qdbg() << "onDataIn: " << strRx;
                                    }
                                    if ( dsIn.commitTransaction() == false ) {
                                        return;
                                    }
                                }
                                

                                clsModFileIO is derived from clsModHelper. clsJSON::mscintQtVersion is defined as:

                                const int clsJSON::mscintQtVersion      = QDataStream::Qt_5_14;
                                
                                JonBJ KroMignonK 2 Replies Last reply
                                0
                                • SPlattenS SPlatten

                                  @Bonnie , @jsulm , @KroMignon , this is my send function:

                                  void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                      if ( isOpen() != true ) {
                                          return;
                                      }
                                      //Associate this TCP socket with the output data stream
                                      QByteArray arybytMsg;
                                      QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                                      //dsOut.setDevice(this);
                                      dsOut.setVersion(clsJSON::mscintQtVersion);
                                      //Send message to data stream
                                      dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                      //Write message
                                      write(arybytMsg);
                                  }
                                  

                                  And receiving:

                                  void clsModFileIO::onDataIn() {
                                      QDataStream dsIn;
                                      dsIn.setDevice(this);
                                      dsIn.setVersion(clsJSON::mscintQtVersion);
                                  
                                      QString strRx;
                                      dsIn.startTransaction();
                                      dsIn >> strRx;
                                  
                                      if ( strRx.isEmpty() != true ) {
                                          qdbg() << "onDataIn: " << strRx;
                                      }
                                      if ( dsIn.commitTransaction() == false ) {
                                          return;
                                      }
                                  }
                                  

                                  clsModFileIO is derived from clsModHelper. clsJSON::mscintQtVersion is defined as:

                                  const int clsJSON::mscintQtVersion      = QDataStream::Qt_5_14;
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #21

                                  @SPlatten
                                  But you are sending a QJsonDocument::toJson() which returns a QByteArray and receiving a QString? So....

                                  1 Reply Last reply
                                  1
                                  • SPlattenS SPlatten

                                    @Bonnie , @jsulm , @KroMignon , this is my send function:

                                    void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                        if ( isOpen() != true ) {
                                            return;
                                        }
                                        //Associate this TCP socket with the output data stream
                                        QByteArray arybytMsg;
                                        QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                                        //dsOut.setDevice(this);
                                        dsOut.setVersion(clsJSON::mscintQtVersion);
                                        //Send message to data stream
                                        dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                        //Write message
                                        write(arybytMsg);
                                    }
                                    

                                    And receiving:

                                    void clsModFileIO::onDataIn() {
                                        QDataStream dsIn;
                                        dsIn.setDevice(this);
                                        dsIn.setVersion(clsJSON::mscintQtVersion);
                                    
                                        QString strRx;
                                        dsIn.startTransaction();
                                        dsIn >> strRx;
                                    
                                        if ( strRx.isEmpty() != true ) {
                                            qdbg() << "onDataIn: " << strRx;
                                        }
                                        if ( dsIn.commitTransaction() == false ) {
                                            return;
                                        }
                                    }
                                    

                                    clsModFileIO is derived from clsModHelper. clsJSON::mscintQtVersion is defined as:

                                    const int clsJSON::mscintQtVersion      = QDataStream::Qt_5_14;
                                    
                                    KroMignonK Offline
                                    KroMignonK Offline
                                    KroMignon
                                    wrote on last edited by KroMignon
                                    #22

                                    @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

                                    You have to send and read the same thing if you want it to work!

                                    void clsModFileIO::onDataIn() {
                                        QDataStream dsIn;
                                        dsIn.setDevice(this);
                                        dsIn.setVersion(clsJSON::mscintQtVersion);
                                    
                                        QByteArray jsonArray;
                                        dsIn.startTransaction();
                                        dsIn >> jsonArray;
                                    
                                        QJsonDocument doc;
                                        if ( jsonArray.isEmpty() != true ) {
                                            qdbg() << "onDataIn: " << jsonArray;
                                            doc = QJsonDocument::fromJson(jsonArray);
                                        }
                                        if ( dsIn.commitTransaction() == false ) {
                                            return;
                                        }
                                    }
                                    

                                    [EDIT] This only works if you also change the send:

                                    void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                        if ( isOpen() != true ) {
                                            return;
                                        }
                                        //Associate this TCP socket with the output data stream
                                        QDataStream dsOut;
                                        dsOut.setDevice(this);
                                        dsOut.setVersion(clsJSON::mscintQtVersion);
                                        //Send message to data stream
                                        dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                    }
                                    

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    SPlattenS 2 Replies Last reply
                                    3
                                    • KroMignonK KroMignon

                                      @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

                                      You have to send and read the same thing if you want it to work!

                                      void clsModFileIO::onDataIn() {
                                          QDataStream dsIn;
                                          dsIn.setDevice(this);
                                          dsIn.setVersion(clsJSON::mscintQtVersion);
                                      
                                          QByteArray jsonArray;
                                          dsIn.startTransaction();
                                          dsIn >> jsonArray;
                                      
                                          QJsonDocument doc;
                                          if ( jsonArray.isEmpty() != true ) {
                                              qdbg() << "onDataIn: " << jsonArray;
                                              doc = QJsonDocument::fromJson(jsonArray);
                                          }
                                          if ( dsIn.commitTransaction() == false ) {
                                              return;
                                          }
                                      }
                                      

                                      [EDIT] This only works if you also change the send:

                                      void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                          if ( isOpen() != true ) {
                                              return;
                                          }
                                          //Associate this TCP socket with the output data stream
                                          QDataStream dsOut;
                                          dsOut.setDevice(this);
                                          dsOut.setVersion(clsJSON::mscintQtVersion);
                                          //Send message to data stream
                                          dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                      }
                                      
                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by SPlatten
                                      #23
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • KroMignonK KroMignon

                                        @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

                                        You have to send and read the same thing if you want it to work!

                                        void clsModFileIO::onDataIn() {
                                            QDataStream dsIn;
                                            dsIn.setDevice(this);
                                            dsIn.setVersion(clsJSON::mscintQtVersion);
                                        
                                            QByteArray jsonArray;
                                            dsIn.startTransaction();
                                            dsIn >> jsonArray;
                                        
                                            QJsonDocument doc;
                                            if ( jsonArray.isEmpty() != true ) {
                                                qdbg() << "onDataIn: " << jsonArray;
                                                doc = QJsonDocument::fromJson(jsonArray);
                                            }
                                            if ( dsIn.commitTransaction() == false ) {
                                                return;
                                            }
                                        }
                                        

                                        [EDIT] This only works if you also change the send:

                                        void clsModHelper::sendJSON(QJsonObject& objJSON) {
                                            if ( isOpen() != true ) {
                                                return;
                                            }
                                            //Associate this TCP socket with the output data stream
                                            QDataStream dsOut;
                                            dsOut.setDevice(this);
                                            dsOut.setVersion(clsJSON::mscintQtVersion);
                                            //Send message to data stream
                                            dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                                        }
                                        
                                        SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by
                                        #24

                                        @KroMignon , thank you, now its working!

                                        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