HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

QJsonView: A QWidget-based json explorer for Qt

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
explorerjsonqjsonviewforbasedqwidget

Problem

I'm developing applications using Qt which highly make usage of the JSON language to communicate, store and load data of different types. I often need a simple viewer similar to the Firebug JSON explorer to view this data. I already had a JSON parser and serializer called QJson. (update: I also posted my QJson class on Code Review.)

I think this code snippet might be of public interest and there are some open problems (see below) which might be solved by you. I for myself would be glad if the problems get solved or the code will be improved in another point, but I don't need it really. So it is up to you (of course) if you want to review / test / improve the code and contribute.

Features / Preview

QJsonView is a QWidget so you can embed it in any other widget. You can set the value as a JSON-serialized string or as a hierarchical QVariant. It performs syntax-highlighting using HTML and displays this HTML using a QLabel.

An example usage might look like this:

QString data = "{"
                   "\"test\" :  [\"this\", \"is\", \"a\", "
                                 "{\"test\" : [\"with\", \"nested\", \"items\"]}],"
                   "\"types\" : [1337, 13.37, true, null]"
               "}";
QJsonView *jsonView = new QJsonView(this);
jsonView->setJsonValue(data);




Preview: Initial view

You can expand a JSON object or array (QVariantMap or QVariantList respectively) by clicking on the [+] sign. This can also be done from within your code. The entries then are displayed one below another and can be expanded again if they are objects or arrays. Expanding a nested element from within your code is currently not supported.

jsonView->expand();


By enabling hover effects, QJsonView highlights the entry the mouse is currently over:

jsonView->setHoverEffects(true);




Preview: Expanded view with hover effects

From within the context menu, the user can copy the JSON-serialized representation into the clipboard. If this is performe

Solution

I only see minor suggestions so far.

bool hoverEffects();


should be

bool hoverEffects() const;


and likewise for isExpandable, isExpanded.

Depending on your version of C++, there is some syntactic sugar to simplify your map iterator in variantToHtml; do some reading about auto.

Otherwise I don't see anything glaring.

Code Snippets

bool hoverEffects();
bool hoverEffects() const;

Context

StackExchange Code Review Q#11849, answer score: 2

Revisions (0)

No revisions yet.