Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

DataStream in pythonQt



  • 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)
    

  • Lifetime Qt Champion

    Hi,

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



  • 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 :(



  • 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;
        };
    


  • 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



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


  • Lifetime Qt Champion

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



  • 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 ?


Log in to reply