Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. editable Jason file tree
Forum Updated to NodeBB v4.3 + New Features

editable Jason file tree

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 3.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • TrilecT Offline
    TrilecT Offline
    Trilec
    wrote on last edited by
    #1

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

    If ts not in the computer it doesn't exist!...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      TrilecT 1 Reply Last reply
      0
      • mrjjM mrjj

        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.

        TrilecT Offline
        TrilecT Offline
        Trilec
        wrote on last edited by
        #3

        @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"
        

        If ts not in the computer it doesn't exist!...

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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?

          TrilecT 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            TrilecT 1 Reply Last reply
            1
            • VRoninV VRonin

              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

              TrilecT Offline
              TrilecT Offline
              Trilec
              wrote on last edited by
              #6

              @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.

              If ts not in the computer it doesn't exist!...

              1 Reply Last reply
              0
              • mrjjM mrjj

                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?

                TrilecT Offline
                TrilecT Offline
                Trilec
                wrote on last edited by
                #7

                @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"
                                 }
                			]
                       }
                    }			
                }
                

                If ts not in the computer it doesn't exist!...

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  TrilecT 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    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.

                    TrilecT Offline
                    TrilecT Offline
                    Trilec
                    wrote on last edited by
                    #9

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

                    cheers

                    If ts not in the computer it doesn't exist!...

                    mrjjM 1 Reply Last reply
                    0
                    • TrilecT Trilec

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

                      cheers

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved