mgear.vendor.qjsonmodel module

Python adaptation of https://github.com/dridk/QJsonModel

Supports Python 2 and 3 with PySide, PySide2, PyQt4 or PyQt5. Requires https://github.com/mottosso/Qt.py

Usage:

Use it like you would the C++ version.

>>> import qjsonmodel
>>> model = qjsonmodel.QJsonModel()
>>> model.load({"key": "value"})
Test:

Run the provided example to sanity check your Python, dependencies and Qt binding.

$ python qjsonmodel.py

Changes:

This module differs from the C++ version in the following ways.

  1. Setters and getters are replaced by Python properties

  2. Objects are sorted by default, disabled via load(sort=False)

  3. load() takes a Python dictionary as opposed to a string or file handle.

    • To load from a string, use built-in json.loads()
      >>> import json
      >>> document = json.loads("{'key': 'value'}")
      >>> model.load(document)
      
    • To load from a file, use with open(fname)
      >>> import json
      >>> with open("file.json") as f:
      ...    document = json.load(f)
      ...    model.load(document)
      
class mgear.vendor.qjsonmodel.QJsonModel(*args: Any, **kwargs: Any)

Bases: QAbstractItemModel

clear()
columnCount(parent=PySide2.QtCore.QModelIndex)
data(index, role)
flags(index)
genJson(item)
headerData(section, orientation, role)
index(row, column, parent=PySide2.QtCore.QModelIndex)
json(root=None)

Serialise model as JSON-compliant dictionary

Parameters:

root (QJsonTreeItem, optional) – Serialise from here defaults to the the top-level item

Returns:

model as dict

load(document)

Load from dictionary

Parameters:

document (dict) – JSON-compatible dictionary

parent(index)
rowCount(parent=PySide2.QtCore.QModelIndex)
setData(index, value, role)
class mgear.vendor.qjsonmodel.QJsonTreeItem(parent=None)

Bases: object

appendChild(item)
child(row)
childCount()
property key
classmethod load(value, parent=None, sort=True)
parent()
row()
property type
property value