Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. DataStream in pythonQt

DataStream in pythonQt

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 3 Posters 2.1k 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by RahibeMeryem
    #1

    I have below code in C++ (Qt) and want to receive from python and back to the c++ again. I have lack of python knowledge any help appreciated: using mqtt
    PS: if another way is faster and practical appreciated:

    data struct I want to pass between c++ - python

    struct dataLoad {
            QString text;
            QPixmap image;
        };
    
    QByteArray byteArray_;
    
    QDataStream stream(&byteArray_, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_5_10);
    
    stream << dl->image << dl->text;
    
    if (messqt->m_client->publish(messqt->topic,
                                          byteArray_,
                                         2,
                                          true) == -1)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Not taking into account the dataLoad structure, do you already have the communication running between your C++ and Python application ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by RahibeMeryem
        #3

        Not yet...and honestly we couldnt find a simplest/fastest way to communicate between them :( We have c++ Qt application . and suddenly we need to use a python trained AI model and couldnt convert to c++ . So python app will work as a prediction server waiting images from Qt C++ .

        From Qt C++ we need send images to the python and get back to result

        Qt C++ will send :: image + text

        Python send back : image + text

        what will be the simplest and easy solution ? We stucked here . too much deepLearning math thinking took away our coding skills last two days :(

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RahibeMeryem
          wrote on last edited by
          #4

          I can send data to mqtt server through C++
          But I cant get it from python:

          def on_message(client, userdata, message):
              time.sleep(1)
              data = qt.QByteArray()
              dataStream  = qt.QDataStream(data ,QIODevice.ReadOnly)
          
              try:
                  dataStream = qt.QDataStream(message.payload)
          
                  data.setVersion(qt.QtCore.QDataStream.Qt_5_10)
              except Exception:
                  print("hata")
              print("received message =",str(message.payload.decode("utf-8")))
          
          

          how can I receive data from python and respond
          back properly ?

          struct dataLoad {
                  QString text;
                  QPixmap image;
              };
          
          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            You need to use the QDataStream deserialisation on the python end. For the string should be easy, should just be dataStream.readQString() or dataStream.readString(). Not sure how you do it with QPixmap though

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • R Offline
              R Offline
              RahibeMeryem
              wrote on last edited by
              #6

              yes :) the problem there is both text and binary (Qpixmap or Qımage or cv::mat)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Is that dataLoad struct only used to share data through MQTT ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  RahibeMeryem
                  wrote on last edited by
                  #8

                  Finally I managed like this:

                  block = qt.QByteArray(message.payload)
                         out = qt.QDataStream(block, qt.QIODevice.ReadOnly)
                         out.setVersion(qt.QDataStream.Qt_5_10)
                         png = out.readBytes()
                         text = out.readQString()
                  
                         img_np = cv2.imdecode(np.fromstring(png, np.uint8), cv2.IMREAD_COLOR)  # cv2.IMREAD_COLOR in OpenCV 3.1
                         cv2.imshow("test", img_np)
                  

                  I am sending as PNG from Qt C++ . through mqtt . ie:

                  QByteArray blob = file.readAll();
                                  QByteArray ba;
                                  if (img_path.contains("png")){
                                      //QImage to convert PNG in memory
                                      QImage img(img_path);
                                      QBuffer buffer(&ba);
                                      buffer.open(QIODevice::WriteOnly);
                                      img.save(&buffer, "PNG");
                  
                                  }
                  

                  Bu I am still not sure this is the best way . Because there is mqt server between and things going unpredictable any time which I experienced.

                  python code and the Qt C++ code is in the same machine or at least same switch network.

                  What could be the best way to communicate between them ?

                  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