From f0668433c4041a00399eeb29856273fce1389bf3 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 11 Dec 2020 11:56:48 +0100 Subject: Bindable property with initialization Implement Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS and Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS macros. They allow to directly initialize the property member. Task-number: QTBUG-85520 Change-Id: I76541d6785bbbf27976b9f0b865fb45be1e9beee Reviewed-by: Fabian Kosmale --- .../snippets/code/src_corelib_kernel_qproperty.cpp | 39 ++++++++++++++++++++++ src/corelib/kernel/qproperty.cpp | 18 ++++++++-- src/corelib/kernel/qproperty.h | 23 +++++++++++++ src/corelib/kernel/qproperty_p.h | 9 +++++ 4 files changed, 87 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp index ed5df6eb86..4cb7c1c0ce 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qproperty.cpp @@ -68,3 +68,42 @@ private: Q_OBJECT_BINDABLE_PROPERTY(MyClass, int, xProp, &MyClass::xChanged) }; //! [0] + +//! [1] +class MyClass : public QObject +{ + Q_OBJECT + Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged BINDABLE bindableX) +public: + int x() const { return xProp; } + void setX(int x) { xProp = x; } + QBindable bindableX() { return QBindable(&xProp); } + +signals: + void xChanged(); + +private: + // Declare the instance of int bindable property data and + // initialize it with the value 5. + // This is similar to declaring + // int xProp = 5; + // without using the new QObjectBindableProperty class. + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, int, xProp, 5, &MyClass::xChanged) +}; +//! [1] + +//! [2] +class CustomType +{ +public: + CustomType(int val, int otherVal) : value(val), anotherValue(otherVal) { } + +private: + int value = 0; + int anotherValue = 0; +}; + +// later when using CustomType as a property +Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MyClass, CustomType xProp, CustomType(5, 10), + &MyClass::xChanged) +//! [2] diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 1fd5951675..20efbaa1aa 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -947,8 +947,22 @@ QString QPropertyBindingError::description() const \snippet code/src_corelib_kernel_qproperty.cpp 0 - If the property does not need a changed notification, you can leave out the "NOFITY xChanged" in the Q_PROPERTY macro as well as the last argument - of the Q_OBJECT_BINDABLE_PROPERTY macro. + If you need to directly initialize the property with some non-default value, + you can use the Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS macro. It accepts a + value for the initialization as one of its parameters. + + \snippet code/src_corelib_kernel_qproperty.cpp 1 + + Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS does not support multiple arguments + directly. If your property requires multiple arguments for initialization, + please explicitly call the specific constructor. + + \snippet code/src_corelib_kernel_qproperty.cpp 2 + + If the property does not need a changed notification, you can leave out the + "NOFITY xChanged" in the Q_PROPERTY macro as well as the last argument + of the Q_OBJECT_BINDABLE_PROPERTY and Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS + macros. */ /*! diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index b6653c0411..d7d4593c09 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -1031,6 +1031,29 @@ private: #define Q_OBJECT_BINDABLE_PROPERTY(...) QT_OVERLOADED_MACRO(Q_OBJECT_BINDABLE_PROPERTY, __VA_ARGS__) +#define Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS4(Class, Type, name, value) \ + static constexpr size_t _qt_property_##name##_offset() \ + { \ + QT_WARNING_PUSH QT_WARNING_DISABLE_INVALID_OFFSETOF return offsetof(Class, name); \ + QT_WARNING_POP \ + } \ + QObjectBindableProperty name = \ + QObjectBindableProperty( \ + value); + +#define Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS5(Class, Type, name, value, Signal) \ + static constexpr size_t _qt_property_##name##_offset() \ + { \ + QT_WARNING_PUSH QT_WARNING_DISABLE_INVALID_OFFSETOF return offsetof(Class, name); \ + QT_WARNING_POP \ + } \ + QObjectBindableProperty name = \ + QObjectBindableProperty( \ + value); + +#define Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(...) \ + QT_OVERLOADED_MACRO(Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS, __VA_ARGS__) + template class QObjectComputedProperty : public QUntypedPropertyData { diff --git a/src/corelib/kernel/qproperty_p.h b/src/corelib/kernel/qproperty_p.h index 13bd40fad7..f7792c1b5a 100644 --- a/src/corelib/kernel/qproperty_p.h +++ b/src/corelib/kernel/qproperty_p.h @@ -545,6 +545,15 @@ private: } \ QObjectCompatProperty name; +#define Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(Class, Type, name, setter, value) \ + static constexpr size_t _qt_property_##name##_offset() \ + { \ + QT_WARNING_PUSH QT_WARNING_DISABLE_INVALID_OFFSETOF return offsetof(Class, name); \ + QT_WARNING_POP \ + } \ + QObjectCompatProperty name = \ + QObjectCompatProperty( \ + value); QT_END_NAMESPACE -- cgit v1.2.3