JsonModel for QTreeView
-
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;
}@
-
Hi,
Looks like you could take some inspiration from "QJsonTreeWidget":https://github.com/valerino/QJsonTreeWidget
Hope it helps
-
I made it !
https://github.com/dridk/QJsonmodel
@QJsonModel * model = new QJsonModel;
QTreeView * view = new QTreeView;
view->setModel(model);
model->load("example.json");@enjoy !!
-
Thanks for sharing ! :)
-
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/
-
Thanks :)
For a python version : https://github.com/GrxE/PyQJsonModel
-
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 -
@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-qtBetter link : https://github.com/benlau/qsyncable
It doesn't behave as a tree model though AFAIK. -
J JonB referenced this topic on