diff options
author | Gerhard Gappmeier <gerhard.gappmeier@ascolab.com> | 2012-07-02 13:01:32 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-19 17:36:55 +0100 |
commit | 9bbebb914422262b7b585b6d1dab9d21c4238c44 (patch) | |
tree | 65a87988bb8d2d8bae3e81bbff2dce480f436435 /src/corelib | |
parent | 6f225b0b5d774828df310948435f1cc3a4720104 (diff) |
Add support for defining properties from member variables.
This associates properties with member variables and
avoids writing getter and setter methods manually.
The metaCall() method directly accesses the member variable,
so additional method calls can be avoided.
The metaCall() setter code also supports NOTIFY signals,
which means the according signal is emitted when the property
gets written.
Task-number: QTBUG-16852
Change-Id: I88a1f237ea53a1e9cf65fc9ef2e207718eb8b6c3
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/doc/snippets/code/doc_src_properties.cpp | 21 | ||||
-rw-r--r-- | src/corelib/doc/src/objectmodel/properties.qdoc | 26 |
2 files changed, 40 insertions, 7 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_properties.cpp b/src/corelib/doc/snippets/code/doc_src_properties.cpp index 7ee414e00e..a67cbb68aa 100644 --- a/src/corelib/doc/snippets/code/doc_src_properties.cpp +++ b/src/corelib/doc/snippets/code/doc_src_properties.cpp @@ -40,8 +40,8 @@ //! [0] Q_PROPERTY(type name - READ getFunction - [WRITE setFunction] + (READ getFunction [WRITE setFunction] | + MEMBER memberName [(READ getFunction | WRITE setFunction)]) [RESET resetFunction] [NOTIFY notifySignal] [REVISION int] @@ -130,3 +130,20 @@ object->setProperty("priority", "VeryHigh"); //! [7] Q_CLASSINFO("Version", "3.0.0") //! [7] + +//! [8] + Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged) + Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged) + Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged) + ... +signals: + void colorChanged(); + void spacingChanged(); + void textChanged(const QString &newText); + +private: + QColor m_color; + qreal m_spacing; + QString m_text; +//! [8] + diff --git a/src/corelib/doc/src/objectmodel/properties.qdoc b/src/corelib/doc/src/objectmodel/properties.qdoc index 1e88a67a90..e262adf886 100644 --- a/src/corelib/doc/src/objectmodel/properties.qdoc +++ b/src/corelib/doc/src/objectmodel/properties.qdoc @@ -53,16 +53,22 @@ \snippet code/doc_src_properties.cpp 1 + Here is an example showing how to export member variables as Qt + properties using the \c MEMBER keyword. + Note that a \c NOTIFY signal must be specified to allow QML property bindings. + + \snippet code/doc_src_properties.cpp 8 + A property behaves like a class data member, but it has additional features accessible through the \l {Meta-Object System}. \list - \li A \c READ accessor function is required. It is for reading the - property value. Ideally, a const function is used for this purpose, - and it must return either the property's type or a pointer or - reference to that type. e.g., QWidget::focus is a read-only property - with \c READ function, QWidget::hasFocus(). + \li A \c READ accessor function is required if no \c MEMBER variable was + specified. It is for reading the property value. Ideally, a const function + is used for this purpose, and it must return either the property's type or a + pointer or reference to that type. e.g., QWidget::focus is a read-only + property with \c READ function, QWidget::hasFocus(). \li A \c WRITE accessor function is optional. It is for setting the property value. It must return void and must take exactly one @@ -71,6 +77,13 @@ QWidget::setEnabled(). Read-only properties do not need \c WRITE functions. e.g., QWidget::focus has no \c WRITE function. + \li A \c MEMBER variable association is required if no \c READ accessor + function is specified. This makes the given member variable + readable and writable without the need of creating \c READ and \c WRITE accessor + functions. It's still possible to use \c READ or \c WRITE accessor functions in + addition to \c MEMBER variable association (but not both), if you need to + control the variable access. + \li A \c RESET function is optional. It is for setting the property back to its context specific default value. e.g., QWidget::cursor has the typical \c READ and \c WRITE functions, QWidget::cursor() @@ -82,6 +95,9 @@ \li A \c NOTIFY signal is optional. If defined, it should specify one existing signal in that class that is emitted whenever the value of the property changes. + \c NOTIFY signals for \c MEMBER variables must take zero or one parameter, + which must be of the same type as the property. The parameter will take the + new value of the property. \li A \c REVISION number is optional. If included, it defines the property and its notifier signal to be used in a particular |