Git Product home page Git Product logo

qt-json-view's Introduction

Qt JSON View

This widget allows to display and edit JSON-serializable data in a Qt view. The system is easily extensible with custom types.

An example to get you started is here.

Qt JSON View

Overview

A provided JSON-serializable dict or list is converted to a JsonModel derived from QStandardItemModel. During conversion, each entry in the source data is mapped to a DataType. The DataType defines how the entry is added to the JsonModel, how it is serialized. The JsonDelegate draws on the optional DataType implementations to display the item. The DataType can also define custom right-click QActions for the item. The JsonModel can then be serialized back into a dictionary after editing.

DataTypes

A number of data types are already implemented, but it is easy to implement and inject your own on the fly, please see section below.

Standard JSON Types:

Custom Types:

  • UrlType: Detects urls and provides an "Explore ..." action opening the web browser.
  • FilepathType: Detects file paths and provides an "Explore ..." action opening the file browser
  • RangeType: A range is displayed in one row and has to be a dict in the form of, both floats and ints are allowed and displayed accordingly:
{
    "start": 0,
    "end": 100,
    "step": 2.5
}
  • ChoicesType: The user can choose from a range of choices. It is shown as a combobox. The data has to be a dict in the form:
{
    "value": "A",
    "choices": ["A", "B", "C"]
}

Implement custom DataTypes

Subclass the DataType base class and implement what you need, at least the matches method. Then inject an instance of your DataType into datatypes.DATA_TYPES so it is found when the model is initialized. Make sure to inject it at the right position in the list datatypes.DATA_TYPES list since the model uses the first match it finds.

from qt_json_view import datatypes

class TestType(object):

    def matches(self, data):
        if data == "TEST":
            return True
        return False

idx = [i for i in datatypes.DATA_TYPES if isinstance(i, datatypes.StrType)][0]
datatypes.DATA_TYPES.insert(idx, TestType())

View

The JsonView is a QTreeView with the delegate.JsonDelegate.

Model

The JsonModel is a QStandardItemModel. It can be initialized from a JSON-serializable object and serialized to a JSON-serializable object.

Filtering

The JsonSortFilterProxyModel is a QSortFilterProxyModel extended to filter through the entire tree.

Delegate

The JsonDelegate draws on the DataTypes of the items to determine how they are drawn. The DataType uses the paint, createEditor and setModelData methods if they are available on the DataType.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.