editable Jason file tree



  • Hi All, I'm hoping a bright QT users may be able to shed some light on a small example I'm having trouble with.
    my goal was to take the editable tree view example and expand upon this to read Json files, I'm sure there are other ways to approach this but the focus was keeping it fairly simple and in line with the existing QT example:

    The reading of the Jason file works up until the point it's injecting the treeItem into the model.
    and at this point the QTVariant reports an invalid entry, this of course means there is nothing displayed in the tree except the first entry.
    Data: QVariant(Invalid) QVariant(Invalid)

    I'm unsure whether problem is coming from but feel it could be related to my use of the QTVariant*
    (As is basically the editable Treeview found in the example section I don't feel it's necessary to post the entire code)

    Some headers required:

    #ifndef JSONMODEL_H
    #define JSONMODEL_H
    
    
    #include <QAbstractItemModel>
    #include <QModelIndex>
    #include <QVariant>
    #include <QJsonDocument>
    #include <QJsonValue>
    #include <QJsonArray>
    #include <QJsonObject>
    #include <QIcon>
    #include <QList>
    #include <QVariant>
    #include <QVector>
    

    The main code snip:

    QString TreeModel::processJsonType(QJsonValue mVal)
    {
        switch(mVal.type() ){
            case QJsonValue::Object : return "Object";
            case QJsonValue::Bool : return "Bool";
            case QJsonValue::String : return "String";
            case QJsonValue::Double : return "Double";
            case QJsonValue::Array : return "Array";
            case QJsonValue::Null : return "Null";
            default: return "Undefined";
        }
    }
    
    
    TreeItem* TreeModel::processJson(const QJsonValue& value, TreeItem *parent)
    {
        TreeItem * item = new TreeItem(parent);
        item->insertColumns(0,2);
    
        if(parent !=0 )  qDebug() << "Data: " << parent->data(0) << " " << parent->data(1);
    
        if ( value.isObject())
        {
    
            //Get all QJsonValue childs
            for (QString key : value.toObject().keys()){
                QJsonValue v = value.toObject().value(key);
                TreeItem * child = processJson(v, item);
                child->insertColumns(0,2);
                child->setData(0,key);
                child->setData(1,processJsonType(v));
                item->appendChild(child);
            }
    
        }
        else if ( value.isArray())
        {
            //Get all QJsonValue childs
            int index = 0;
            for (QJsonValue v : value.toArray()) {
                TreeItem * child = processJson(v,item);
                child->insertColumns(0,2);
                child->setData(0, QString::number(index));
                item->setData(1,"isArray"); //Test
                item->appendChild(child);
                ++index;
            }
        }
        else
        {
            item->setData(0,value.toVariant().toString());
        }
    
     return item;
    }
    
    bool TreeModel::loadFromJson(const QJsonDocument &jsonDocument)
    {
    
        if (jsonDocument.isNull() ) {
            qDebug()<<Q_FUNC_INFO<<"cannot load json";
            return false;
        }
        //setup incomming document first
        QJsonValue jVal;
    
        if ( jsonDocument.isArray() ) {
            jVal = QJsonValue(jsonDocument.array());
        }
        else {
            jVal = QJsonValue(jsonDocument.object());
        }
    
        //process the object and arrays
        beginResetModel();
    
        rootItem = processJson( jVal, rootItem );
        rootItem->setData(0, "Node"); //the column data at 0index
        rootItem->setData(1, "Type"); //the column data at 1index
    
        endResetModel();
        return true;
    }
    
    
    

  • Qt Champions 2016

    Hi
    Its hard to guess at when not running the code.
    Did you check what you are actually inserting ?

    item->setData(0,value.toVariant().toString());
    qDebug() << "INS" << value.toVariant().toString();

    and also print it the other insertion points to see if you can spot when values goes invalid.



  • @mrjj , yes values seem to be valid, also the tree children placement is correct , but nothing shows in the tree values (except the first top value . ie first one in top level tree)

    INS "-24.049999237060547"
    INS "false"
    INS "camplt_86"
    INS "-24328"
    INS "false"
    INS "camplt_86CARD"
    INS "seq"
    

  • Qt Champions 2016

    Hi
    Ok so the data is valid it seems.

    Can it be wrongly inserted ?

    I mean when you call
    processJson(const QJsonValue& value, TreeItem *parent)
    it is most likely recursive.
    So i wonder if parent is updated for each level?



  • This project: https://github.com/dridk/QJsonModel should be a good starting point. It's read only so you'll have to implement the editing part but it's a start



  • @VRonin said in editable Jason file tree:

    This project: https://github.com/dridk/QJsonModel should be a good starting point. It's read only so you'll have to implement the editing part but it's a start

    Thanks VRonin , I had already seen this and use as a guide for the current implementation.



  • @mrjj it appears the data is not valid, but not sure why

    a small qDebug

    ...
    TreeItem* TreeModel::processJson(const QJsonValue& value, TreeItem *parent)
    {
        TreeItem * item = new TreeItem(parent);
        item->insertColumns(0,2);
    
        if(parent !=0 )  qDebug() << "Data: " << parent->data(0) << " " << parent->data(1);
    
        if ( value.isObject())
    ....
    

    output:

    Data:  QVariant(QString, "Node")   QVariant(QString, "Type")
    Data:  QVariant(Invalid)   QVariant(Invalid)
    Data:  QVariant(Invalid)   QVariant(Invalid)
    Data:  QVariant(Invalid)   QVariant(Invalid)
    Data:  QVariant(Invalid)   QVariant(Invalid)
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "-24.049999237060547"
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "false"
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "camplt_86_0910zsqu"
    Data:  QVariant(Invalid)   QVariant(QString, "isArray")
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "-24328"
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "false"
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "camplt_86_0910zsquCARD"
    Data:  QVariant(Invalid)   QVariant(Invalid)
    INS "sequence"
    

    JSON file

    {
        "BB_003":
        {
            "data":
            {
                "type": "seq",
                 "forms": [
                    {
                        "AngleZ": -24.049999237060547,
                        "Handness": false,
                        "Name": "86_0910TEST"
                     },
                    {
                        "AngleZ": -24328,
                        "Handness": false,
                        "Name": "camp_86_0910CARD"
                     }
    			]
           }
        }			
    }
    

  • Qt Champions 2016

    Hi
    I think its due to calling processJson recursively and im not sure its the right/expected parent
    when you do parent->data(0)
    But only using the debugger, it would be clear.

    You really should make a small json file and single step the reading/adding of it to find out
    what could be wrong. Inspecting each value as it happens.



  • @mrjj
    thanks, I did a small json but the debugger had issues, ill try to resolve this and try again.

    cheers


  • Qt Champions 2016

    @Trilec
    Ok, hopefully its easy to spot once you single step.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.