JsonTreeModel Implementation
v0.1.0
A Qt tree model for visualizing and manipulating a JSON document.
|
The JsonTreeModel class provides a data model for a JSON document. More...
#include <jsontreemodel.h>
Public Types | |
enum | ScalarColumnSearchMode { NoSearch, QuickSearch, ComprehensiveSearch } |
This enum controls how setJson() updates the model's column headers. More... | |
Public Member Functions | |
JsonTreeModel (QObject *parent=nullptr) | |
Constructs an empty JsonTreeModel with the given parent. More... | |
~JsonTreeModel () override | |
Destroys the JsonTreeModel and frees its memory. More... | |
QVariant | headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override |
QModelIndex | index (int row, int column, const QModelIndex &parent=QModelIndex()) const override |
QModelIndex | parent (const QModelIndex &index) const override |
int | rowCount (const QModelIndex &parent=QModelIndex()) const override |
Returns the number of rows under the given parent. More... | |
int | columnCount (const QModelIndex &parent=QModelIndex()) const override |
Returns the number of columns in the model. More... | |
QVariant | data (const QModelIndex &index, int role=Qt::DisplayRole) const override |
Returns data under the given index for the specified role. More... | |
bool | setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override |
Qt::ItemFlags | flags (const QModelIndex &index) const override |
void | setJson (const QJsonArray &array, ScalarColumnSearchMode searchMode=QuickSearch) |
Sets the whole model's internal data structure to the given JSON array. More... | |
void | setJson (const QJsonObject &object, ScalarColumnSearchMode searchMode=QuickSearch) |
Sets the whole model's internal data structure to the given JSON object. More... | |
QJsonValue | json (const QModelIndex &index=QModelIndex()) const |
Returns the JSON value under the given index. More... | |
void | setScalarColumns (const QStringList &columns) |
Sets the JSON objects' scalar members that are shown by the model. More... | |
QStringList | scalarColumns () const |
Returns the names of the JSON objects' scalar members that are shown by the model. More... | |
The JsonTreeModel class provides a data model for a JSON document.
JsonTreeModel represents an arbitrary JSON document as a tree. It supports unlimited nesting of JSON arrays and JSON objects. For compactness, scalar members of JSON objects are placed under named columns.
For example, the following JSON document contains an array of similar objects which can be compacted into a table:
QTableView::hideColumn()
or QTreeView::hideColumn()
to hide the empty Scalar columnHere is a more hierarchical example:
This enum controls how setJson() updates the model's column headers.
Enumerator | |
---|---|
NoSearch | setJson() leaves the model headers unchanged. The caller should manually update the column headers via setScalarColumns(). |
QuickSearch | setJson() does a quick scan on the new JSON document. The model scans every member of JSON objects, but only scans the first element of JSON arrays. |
ComprehensiveSearch | setJson() does a full scan on the new JSON document. The model scans every member of JSON objects and JSON arrays. All scalar members will be found and displayed, but this could be expensive for large JSON documents. |
|
explicit |
Constructs an empty JsonTreeModel with the given parent.
|
inlineoverride |
Destroys the JsonTreeModel and frees its memory.
|
override |
Returns the number of columns in the model.
This number is the same for the entire model; the parent is irrelevant.
|
override |
Returns data under the given index for the specified role.
Only valid when role is Qt::DisplayRole
or Qt::EditRole
.
If index.column()
is 0, then this function returns the array index or object member name that corresponds to index.row()
. This differs from json(), which returns the full JSON value if index.column()
is 0.
|
override |
|
override |
Horizontal headers show the text of scalarColumns() for the third column onwards. Vertical headers show the text of column 0.
|
override |
QJsonValue JsonTreeModel::json | ( | const QModelIndex & | index = QModelIndex() | ) | const |
Returns the JSON value under the given index.
If the index is invalid, then this function returns the entire JSON document stored in the model.
If index.column()
is 0, then this function returns the full JSON value at index.row()
; the value could be a JSON object or JSON array. If index.column()
is non-zero, this function returns a single scalar value.
Example:
|
override |
|
override |
Returns the number of rows under the given parent.
If the parent represents a JSON array, then the row count equals the number of array elements. If the parent represents a JSON object, then the row count equals the number of child arrays and child objects combined.
|
inline |
Returns the names of the JSON objects' scalar members that are shown by the model.
|
override |
void JsonTreeModel::setJson | ( | const QJsonArray & | array, |
ScalarColumnSearchMode | searchMode = QuickSearch |
||
) |
void JsonTreeModel::setJson | ( | const QJsonObject & | object, |
ScalarColumnSearchMode | searchMode = QuickSearch |
||
) |
void JsonTreeModel::setScalarColumns | ( | const QStringList & | columns | ) |
Sets the JSON objects' scalar members that are shown by the model.
The model's named scalar columns are set to the list of specified columns, in the listed order.
If a scalar member of a JSON object is named after one of these columns, it is shown under that column. Otherwise, the member is hidden.