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. JsonModel for QTreeView
Qt 6.11 is out! See what's new in the release blog

JsonModel for QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 8 Posters 13.5k Views 3 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.
  • D Offline
    D Offline
    dridk
    wrote on last edited by
    #1

    Does Somebody create a jsonModel based on QAbstractItemModel to populate a QTreeView ?
    This is pretty hard ! Tree Model with parent(), index(), rowCount ... makes me crazy ...
    I started briefly... I m asking now, because I know I will fail... :( So, if some of you have a piece of code , thanks you !

    @#include "jsonmodel.h"

    JsonModel::JsonModel(const QJsonDocument &document, QObject *parent)
    {
    mDoc = document;
    }

    JsonModel::~JsonModel()
    {

    }

    QVariant JsonModel::data(const QModelIndex &index, int role) const
    {

    if (role == Qt::DisplayRole)
        return "Test";
    
    return QVariant();
    

    }

    QModelIndex JsonModel::index(int row, int column, const QModelIndex &parent) const
    {

    if (!hasIndex(row,column, parent))
        return QModelIndex();
    
    return createIndex(row,column);
    

    }

    QModelIndex JsonModel::parent(const QModelIndex &child) const
    {

    }

    int JsonModel::rowCount(const QModelIndex &parent) const
    {
    if (!parent.isValid()){

        if (mDoc.isArray())
            return mDoc.array().count();
        if (mDoc.isObject())
            return mDoc.object().count();
        else
            return 0;
    }
    
    else {
    
    
    
    }
    

    }

    int JsonModel::columnCount(const QModelIndex &parent) const
    {

    return 1;
    

    }@

    Nothing in Biology Makes Sense Except in the Light of Evolution

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

      Hi,

      Looks like you could take some inspiration from "QJsonTreeWidget":https://github.com/valerino/QJsonTreeWidget

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dridk
        wrote on last edited by
        #3

        I made it !

        https://github.com/dridk/QJsonmodel

        @QJsonModel * model = new QJsonModel;
        QTreeView * view = new QTreeView;
        view->setModel(model);
        model->load("example.json");@

        enjoy !!

        Nothing in Biology Makes Sense Except in the Light of Evolution

        Aleksey_KA J 2 Replies Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Thanks for sharing ! :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            Great work! Looks very useful.

            If you’re interested, here’s the work of another community member, who made a model for QVariantMap: http://qt-project.org/forums/viewthread/52418/

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • D dridk

              I made it !

              https://github.com/dridk/QJsonmodel

              @QJsonModel * model = new QJsonModel;
              QTreeView * view = new QTreeView;
              view->setModel(model);
              model->load("example.json");@

              enjoy !!

              Aleksey_KA Offline
              Aleksey_KA Offline
              Aleksey_K
              wrote on last edited by
              #6

              @dridk thanks! Saves much time to me!

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hizoka
                wrote on last edited by
                #7

                Thanks :)

                For a python version : https://github.com/GrxE/PyQJsonModel

                1 Reply Last reply
                0
                • D dridk

                  I made it !

                  https://github.com/dridk/QJsonmodel

                  @QJsonModel * model = new QJsonModel;
                  QTreeView * view = new QTreeView;
                  view->setModel(model);
                  model->load("example.json");@

                  enjoy !!

                  J Offline
                  J Offline
                  justHappy
                  wrote on last edited by
                  #8

                  @dridk Thank you very much!

                  1 Reply Last reply
                  0
                  • GTDevG Offline
                    GTDevG Offline
                    GTDev
                    wrote on last edited by
                    #9

                    I think JsonListModel (QSyncable implementation) is the best json model at the moment. It is only compatible with ListView, GridView and Repeater though (I think):
                    https://v-play.net/apps/avoid-cpp-models-qt

                    Senior Developer at Felgo - https://felgo.com/qt

                    Develop mobile Apps for iOS & Android with Qt
                    Felgo is an official Qt Technology Partner

                    GrecKoG 1 Reply Last reply
                    0
                    • GTDevG GTDev

                      I think JsonListModel (QSyncable implementation) is the best json model at the moment. It is only compatible with ListView, GridView and Repeater though (I think):
                      https://v-play.net/apps/avoid-cpp-models-qt

                      GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #10

                      @GTDev said in JsonModel for QTreeView:

                      I think JsonListModel (QSyncable implementation) is the best json model at the moment. It is only compatible with ListView, GridView and Repeater though (I think):
                      https://v-play.net/apps/avoid-cpp-models-qt

                      Better link : https://github.com/benlau/qsyncable
                      It doesn't behave as a tree model though AFAIK.

                      1 Reply Last reply
                      0
                      • JonBJ JonB referenced this topic on

                      • Login

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