C

Property Struct

template <typename T> struct Qul::Property

The Property class can be used to hold a property of a given type. More...

Header: #include <qul/property.h>
Since: Qt Quick Ultralite 1.0

Public Functions

Property(const T &value)
Property()
void setValue(const T &v)
const T &value()
const T &value() const

Detailed Description

Use this class as a public member of objects you define in C++. They are then exposed as properties of that object in QML. Template parameter of the class defines the C++ type and is mapped to a corresponding QML type. Property type T that does not have built-in comparison operator must be provided with a user-defined operator==.

struct MyData : public Qul::Object
{
    Qul::Property<int> val;
    void update(int x)
    {
        // can get and set property values from C++
        val.setValue(x);
    }
};

The properties defined in C++ can be used in QML bindings.

Item {
    Item {
        // can bind QML property to exported property
        x: mydata_x.val
        color: "red"
        width: 50
        height: 50
    }
    MyData {
        id: mydata_x
        val: 100
    }
    MyData {
        id: mydata_width
        // can bind exported property
        val: parent.width
    }
    Component.onCompleted: {
        mydata_x.update(200);
        console.log(mydata_width.val);
    }
}

As you can see, QML bindings can be assigned to properties defined in C++. The value of the property will automatically update when the properties used in the binding change. They can also be used as sources when defining bindings in QML.

Grouped properties

Properties can be grouped together as follows:

struct MyObject : public Qul::Object
{
    struct Grouped
    {
        Qul::Property<int> val1;
        Qul::Property<int> val2;
    };
    Grouped group;
};

Then they can be used in QML like this:

Item {
    MyObject {
        group.val1: 42
        group.val2: 43
    }
}

The grouping happens by placing the properties inside a struct or class S and then having a field of type S inside the exported class. The type S must not itself be derived from Qul::Object. Only its public fields of type Qul::Property are exposed as properties within the group.

See also Properties and Grouped properties.

Member Function Documentation

Property::Property(const T &value)

Creates a property initialized with value.

Property::Property()

Creates a property holding a zero-initialized value.

void Property::setValue(const T &v)

Sets the value of this binding to v.

See also value().

const T &Property::value()

Gets the value of this property.

See also setValue().

const T &Property::value() const

This is an overloaded function.

Available under certain Qt licenses.
Find out more.