unity.scopes.qt.QVariantBuilder

Helper class for creating and populating QVariant containers. More...

#include <unity/scopes/qt/QVariantBuilder.h>

Public Member Functions

void add_tuple (std::initializer_list< QPair< QString, QVariant >> const &tuple)
 Adds a tuple of key-value pairs to an array. More...
 
void add_tuple (QVector< QPair< QString, QVariant >> const &tuple)
 Adds a tuple of key-value pairs to an array. More...
 
QVariant end ()
 Retrieves a completed variant. More...
 
Copy and assignment

Copy and assignment operators (move and non-move versions) have the usual value semantics.

 QVariantBuilder (QVariantBuilder const &other)
 
 QVariantBuilder (QVariantBuilder &&other)
 
QVariantBuilderoperator= (QVariantBuilder const &other)
 
QVariantBuilderoperator= (QVariantBuilder &&other)
 

Detailed Description

Helper class for creating and populating QVariant containers.

The main purpose of this class is to ease creation of variant containers needed for QPreviewWidget instances or any other classes that require non-trivial variant definitions.

For example, the value of the "rating" key of the following JSON template

{
<span class="stringliteral">&quot;type&quot;</span>: <span class="stringliteral">&quot;reviews&quot;</span>,
...
<span class="stringliteral">&quot;reviews&quot;</span>: [{<span class="stringliteral">&quot;rating&quot;</span>: null, <span class="stringliteral">&quot;review&quot;</span>: null, <span class="stringliteral">&quot;author&quot;</span>: null}]
}

can be created with QVariantBuilder as follows:

VariantBuilder builder;
builder.add_tuple({{<span class="stringliteral">&quot;rating&quot;</span>, QVariant()}, {<span class="stringliteral">&quot;review&quot;</span>, QVariant()}, {<span class="stringliteral">&quot;author&quot;</span>, QVariant()}});

Member Function Documentation

void QVariantBuilder::add_tuple ( std::initializer_list< QPair< QString, QVariant >> const &  tuple)

Adds a tuple of key-value pairs to an array.

It can be used multiple times to create an array of tuples, for example:

[{<span class="stringliteral">&quot;a&quot;</span>: 1, <span class="stringliteral">&quot;b&quot;</span>: 2}, {<span class="stringliteral">&quot;c&quot;</span>: 2, <span class="stringliteral">&quot;d&quot;</span> : 3}]

can be created with:

QVariantBuilder builder;
builder.add_tuple({{<span class="stringliteral">&quot;a&quot;</span>, QVariant(1)}, {<span class="stringliteral">&quot;b&quot;</span>, QVariant(2)}});
builder.add_tuple({{<span class="stringliteral">&quot;c&quot;</span>, QVariant(2)}, {<span class="stringliteral">&quot;d&quot;</span>, QVariant(3)}});
void QVariantBuilder::add_tuple ( QVector< QPair< QString, QVariant >> const &  tuple)

Adds a tuple of key-value pairs to an array.

This is an overloaded version of add_tuple that accepts QVector instead of std::initializer_list.

QVariant QVariantBuilder::end ( )

Retrieves a completed variant.

Returns the completed variant and resets this builder, so the builder can be re-used.

Returns
The completed variant.
Exceptions
unity::LogicExceptionif the builder does not hold a variant.