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 [VIRTUAL]
20 [OVERRIDE]
21 [REQUIRED])
22//! [0]
23
24
25//! [1]
26Q_PROPERTY(bool focus READ hasFocus)
27Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
28Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor)
29//! [1]
30
31//! [2]
32Q_PROPERTY(QDate date READ getDate WRITE setDate)
33//! [2]
34#endif
35
36#if __has_include(<QPushButton>)
37#include <QPushButton>
38void button_example()
39{
40 //! [3]
41 QPushButton *button = new QPushButton;
42 QObject *object = button;
43
44 button->setDown(true);
45 object->setProperty("down", true);
46 //! [3]
47}
48#endif
49
50#include <QMetaProperty>
52{
53 //! [4]
54 QObject *object = new QObject;
55 const QMetaObject *metaobject = object->metaObject();
56 int count = metaobject->propertyCount();
57 for (int i=0; i<count; ++i) {
58 QMetaProperty metaproperty = metaobject->property(i);
59 const char *name = metaproperty.name();
60 QVariant value = object->property(name);
61 //...
62 }
63 //! [4]
64}
65
66//! [5]
67class MyClass : public QObject
68{
69 Q_OBJECT
70 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
71
72public:
73 MyClass(QObject *parent = nullptr);
75
78
80 {
81 if (m_priority == priority)
82 return;
83
86 }
88 { return m_priority; }
89
92
93private:
94 Priority m_priority;
95};
96//! [5]
97
98void example(){
99 //! [6]
100 MyClass *myinstance = new MyClass;
101 QObject *object = myinstance;
102
103 myinstance->setPriority(MyClass::VeryHigh);
104 object->setProperty("priority", "VeryHigh");
105 //! [6]
106}
107
108#ifdef QPROPERTY_MACRO
109//! [7]
110Q_CLASSINFO("DefaultProperty", "content")
111//! [7]
112
113//! [8]
114 Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
115 Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
116 Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
117 //...
118signals:
119 void colorChanged();
120 void spacingChanged();
121 void textChanged(const QString &newText);
122
123private:
124 QColor m_color;
125 qreal m_spacing;
126 QString m_text;
127//! [8]
128#endif
void example()
[5]
void qobject_example()
#define __has_include(x)