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

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.setVersion(qt.QDataStream.Qt_5_10)
        # xout << QByteArray(text.encode('utf-8')) << QByteArray((png))
        xout << v
        xox = (b'')
        xox = block.data()
        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)
    stream.setversion(.....)
    
    QVariantMap v_map;
    
    stream  >> v_map;
    

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

    Thanks/Best


  • Lifetime Qt Champion

    Hi,

    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 ?

    Best



  • @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);
    if(iobj.isObject()){
        QJsonObject obj = doc.object();
        for(const  QString & key: obj.keys()){
            v_map[key] = obj[key].toVariant();
        }
    }
    qDebug() << v_map;
    

Log in to reply