qproperty!() { /* proc-macro */ }Expand description
Registers a property to be accessible from QML.
§Requirements
- The property must be defined within a
modorimplblock, annotated withqobject. - 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 type must implement
QPropertyMember. - 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) must be emitted explicitly by any code that changes the property. The framework does not emit it automatically. - 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 a 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 = my_property_changed);
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(&mut 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 a setter or getter and QML will directly read and write
to the member. A Notify signal must be provided. Qt emits it automatically when QML writes
the property, but when Rust code changes the member directly the notify signal must be emitted
explicitly.
A struct containing a member-based property may look like:
#[derive(Default)]
struct Text {
msg: String
}
#[qobject]
impl Text {
qproperty!("message", Member = msg, Notify = message_changed);
#[qsignal]
fn message_changed(&mut 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 parameters. If no
Notify is provided in combination with member, Constant is required.
Expected as a single keyword without an assignment expression.
Default
Marks this as the QML default property. Content placed inside an object literal without an explicit property assignment is written to it. Expected as a single keyword without an assignment expression.