qproperty!() { /* proc-macro */ }Expand description
Registers a property to be accessible from QML.
§Requirements
- The property must be defined within a
modorimplblock, annotated withqobjectorqobject_impl, respectively. - The first parameter is the property name. It must begin with a lower case letter and can only contain letters, numbers and underscores.
- The property must be one of the supported types.
- The return value of the getter (specified via
Readparameter) must match the property type - The value parameter of the setter (specified via
Writeparameter) must match the property type - The member of the
struct(specified viaMemberparameter) must match the property type - A signal indicating any property changes (specified via
Notifyparameter) needs to be emitted by the changing function - Getter and setter methods must be defined within the same
implblock in which the property is declared.
A property may be accessor-based or member-based or mix of both (see the syntax section for details).
§Accessor based property
A pure accessor-based property can be declared together with a range of functions:
qproperty!("myProperty", Read = get_value, Write = set_value, Notify = "myPropertyChanged");
pub fn get_value(&self) -> i32 { self.value }
pub fn set_value(&mut self, value: i32) {
self.value = value;
self.my_property_changed();
}
#[qsignal]
pub fn my_property_changed(&self);The getter method that returns the current value of the property, the setter (if provided) must
take the input value of the property as its first argument (after &mut self).
§Member based property
Member based properties do not require setter nor getter and Qml will directly read and write
to the member. A Notify signal has to be provided and it has to be triggered whenever the
member is changed.
A struct containing a member-based property may look like:
#[derive(Default)]
struct Text {
msg: String
}
#[qobject_impl]
impl Text {
qproperty!("message", Member = msg, Notify = "messageChanged");
#[qsignal]
fn message_changed(&self);
}More information about Qt properties: https://doc.qt.io/qt-6/properties.html.
§Parameters of qproperty!
Name
The first argument is a string literal specifying the name of the Qt property. This is the name under which the property is exposed to QML and should follow the naming rules from requirements.
Read
Specifies the getter method for the property in the format Read = getter_name.
Write
Specifies the setter method for the property in the format Write = setter_name.
Member
Specifies the struct member variable that will be accessed if no getter or setter are provided.
Expected format: Member = var_name.
Notify
Specifies the name of the signal that has to be emitted when the property changes.
Expected format: Notify = "signal_name".
Constant
A constant property is not allowed to have Write or Notify parameter.
Expected as a single keyword without assignment expression.
Default
QML writes to the default property if a property is defined within a object but not assigned to any property. For more information see https://doc.qt.io/qt-6/qtqml-syntax-objectattributes.html