Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
doc_src_properties.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#ifdef QPROPERTY_MACRO
5//! [0]
6Q_PROPERTY(type name
7 (READ getFunction [WRITE setFunction] |
8 MEMBER memberName [(READ getFunction | WRITE setFunction)])
9 [RESET resetFunction]
10 [NOTIFY notifySignal]
11 [REVISION int | REVISION(int[, int])]
12 [DESIGNABLE bool]
13 [SCRIPTABLE bool]
14 [STORED bool]
15 [USER bool]
16 [BINDABLE bindableProperty]
17 [CONSTANT]
18 [FINAL]
19 [REQUIRED])
20//! [0]
21
22
23//! [1]
24Q_PROPERTY(bool focus READ hasFocus)
25Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
26Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor)
27//! [1]
28
29//! [2]
30Q_PROPERTY(QDate date READ getDate WRITE setDate)
31//! [2]
32#endif
33
34#if __has_include(<QPushButton>)
35#include <QPushButton>
36void button_example()
37{
38 //! [3]
39 QPushButton *button = new QPushButton;
40 QObject *object = button;
41
42 button->setDown(true);
43 object->setProperty("down", true);
44 //! [3]
45}
46#endif
47
48#include <QMetaProperty>
50{
51 //! [4]
52 QObject *object = new QObject;
53 const QMetaObject *metaobject = object->metaObject();
54 int count = metaobject->propertyCount();
55 for (int i=0; i<count; ++i) {
56 QMetaProperty metaproperty = metaobject->property(i);
57 const char *name = metaproperty.name();
58 QVariant value = object->property(name);
59 //...
60 }
61 //! [4]
62}
63
64//! [5]
65class MyClass : public QObject
66{
67 Q_OBJECT
68 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
69
70public:
71 MyClass(QObject *parent = nullptr);
73
76
78 {
79 if (m_priority == priority)
80 return;
81
84 }
86 { return m_priority; }
87
90
91private:
92 Priority m_priority;
93};
94//! [5]
95
96void example(){
97 //! [6]
98 MyClass *myinstance = new MyClass;
99 QObject *object = myinstance;
100
101 myinstance->setPriority(MyClass::VeryHigh);
102 object->setProperty("priority", "VeryHigh");
103 //! [6]
104}
105
106#ifdef QPROPERTY_MACRO
107//! [7]
108Q_CLASSINFO("DefaultProperty", "content")
109//! [7]
110
111//! [8]
112 Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
113 Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
114 Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
115 //...
116signals:
117 void colorChanged();
118 void spacingChanged();
119 void textChanged(const QString &newText);
120
121private:
122 QColor m_color;
123 qreal m_spacing;
124 QString m_text;
125//! [8]
126#endif
void example()
[5]
void qobject_example()
#define __has_include(x)