Solved How can I read QVariantMap send from Pyqt5 ia QDataStream ?
-
I have QVariant Map in python:
data = {'mq_username': ('123'), 'np_database': ('png') , 'category': ('png') , 'enroll_name': ('png') , 'notes': ('png') , 'file': ('png') , 'np_database': ('png') , 'PNG-image': (png) } v = QVariant(data) block = QByteArray() xout = QDataStream(block, QIODevice.WriteOnly) xout.setVersion(qt.QDataStream.Qt_5_10) xout << v xox = (b'') xox = block.data() res = client.publish(test_kanali, xox, 2, retain=False)
receving perfectly from mqt in python and can deserialize :
block = qt.QByteArray(message.payload) out = qt.QDataStream(block, qt.QIODevice.ReadOnly) out.setVersion(qt.QDataStream.Qt_5_10) keys = out.readQVariant()
But I cant figure aout how to do in c++ ?
QVariantMap vmat_x; QDataStream stream(message); stream.setVersion(QDataStream::Qt_5_10); stream >> vmat_x ;
vmat_x is alway empty ?
Best
-
@RahibeMeryem
The problem is really at the Python side, not the C++ side. If you intend to share with C++, you cannot afford to write any Python-object-type into the stream; it looks like you have written aPyQt_PyObject
, and that would mean it is only readable by PyQt5. -
@RahibeMeryem said in How can I read QVariantMap send from Pyqt5 ia QDataStream ?:
vmat_x is alway empty ?
Because you try to read a pointer instead an object.
-
-
Hi,
IIRC, you have to read it into a QVariant and then you can extract the QVariantMap.
-
@SGaist said in How can I read QVariantMap send from Pyqt5 ia QDataStream ?:
you have to read it into a QVariant and then you can extract the QVariantMap.
You're right - I've overseen that he adds a QVariant, not a QVariantMap.
-
void MainWindow::mq_message_received(const QByteArray &message, const QMqttTopicName &topic) { //message is the mqt received from Python created QDatastream QDataStream stream(message); stream.setVersion(QDataStream::Qt_5_10); QVariant vmat_t; stream >> vmat_t ; //Convert to QVariantMap }
I have nothing in the vmat_t QbyteArray.
Would you mind to show me an example How can I get this stream as finally QVarianMap ?Best
Thanks -
@RahibeMeryem said in How can I read QVariantMap send from Pyqt5 ia QDataStream ?:
I have nothing in the vmat_t QbyteArray.
First you should make sure that the data you're sending arrive at your receiver by e.g. output the bytearray you send you you receive
-
stream >> vmat_t ; QVariant::load: unknown user type with name PyQt_PyObject.
The error
-
@Christian-Ehrlicher I have python client that read the same data correctly. SO data is arriving properly and can be read from python client as Qvariantmap.
Problem is at the c++ side
Best
-
@RahibeMeryem
The problem is really at the Python side, not the C++ side. If you intend to share with C++, you cannot afford to write any Python-object-type into the stream; it looks like you have written aPyQt_PyObject
, and that would mean it is only readable by PyQt5.