How to receive python QVariant(data_dict(..)) from Qt c++ QDatastream QVariantMap?

  • Hi,

    I am creating QVariantMap in python and sending to mqtt:

    data = {'mq_username': QVariant('123'),  'np_database': QVariant('png') ,  'category': QVariant('png') ,  'enroll_name': QVariant('png') ,  'notes': QVariant('png') ,   'file': QVariant('png') ,
                'np_database': QVariant('png') , 'PNG-image': QVariant(png) }
    v = QVariant(data)
    block = QByteArray()
        xout = QDataStream(block, QIODevice.WriteOnly)
        # xout << QByteArray(text.encode('utf-8')) << QByteArray((png))
        xout << v
        xox = (b'')
        xox =
        res = client.publish(test_kanali, xox, 2, retain=False)

    I try to receive the data from Qt C++ code as below but no succes :

    QDataStream stream(message)
    QVariantMap v_map;
    stream  >> v_map;

    How can I receive python QVariant(dict) from C++ code as above ? Any idea?


  • Lifetime Qt Champion


    What if you load it directly in a QVariant ?

  • @SGaist

    no succes...

    exchanging QVariant between python and C++ could be very usefull..

    Is there anyway to do this ?


  • @RahibeMeryem Why don't you convert it to json?

    import json
    data = {
        "mq_username": "123",
        "np_database": "png",
        "category": "png",
        "enroll_name": "png",
        "notes": "png",
        "file": "png",
        "np_database": "png",
        "PNG-image": "png",
    res = client.publish(test_kanali, json.dumps(data), 2, retain=False)
    QVariantMap v_map;
    QJsonDocument doc = QJsonDocument::fromJson(message);
        QJsonObject obj = doc.object();
        for(const  QString & key: obj.keys()){
            v_map[key] = obj[key].toVariant();
    qDebug() << v_map;

Log in to reply