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//! [0]
5Q_PROPERTY(type name
6 (READ getFunction [WRITE setFunction] |
7 MEMBER memberName [(READ getFunction | WRITE setFunction)])
8 [RESET resetFunction]
9 [NOTIFY notifySignal]
10 [REVISION int | REVISION(int[, int])]
11 [DESIGNABLE bool]
12 [SCRIPTABLE bool]
13 [STORED bool]
14 [USER bool]
15 [BINDABLE bindableProperty]
16 [CONSTANT]
17 [FINAL]
18 [REQUIRED])
19//! [0]
20
21
22//! [1]
23Q_PROPERTY(bool focus READ hasFocus)
24Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
25Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor)
26//! [1]
27
28
29//! [2]
30Q_PROPERTY(QDate date READ getDate WRITE setDate)
31//! [2]
32
33
34//! [3]
35QPushButton *button = new QPushButton;
36QObject *object = button;
37
38button->setDown(true);
39object->setProperty("down", true);
40//! [3]
41
42
43//! [4]
44QObject *object = ...
45const QMetaObject *metaobject = object->metaObject();
46int count = metaobject->propertyCount();
47for (int i=0; i<count; ++i) {
48 QMetaProperty metaproperty = metaobject->property(i);
49 const char *name = metaproperty.name();
50 QVariant value = object->property(name);
51 ...
52}
53//! [4]
54
55
56//! [5]
57class MyClass : public QObject
58{
59 Q_OBJECT
60 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
61
62public:
63 MyClass(QObject *parent = nullptr);
65
68
70 {
71 if (m_priority == priority)
72 return;
73
76 }
78 { return m_priority; }
79
82
83private:
84 Priority m_priority;
85};
86//! [5]
87
88
89//! [6]
91QObject *object = myinstance;
92
93myinstance->setPriority(MyClass::VeryHigh);
94object->setProperty("priority", "VeryHigh");
95//! [6]
96
97
98//! [7]
99Q_CLASSINFO("DefaultProperty", "content")
100//! [7]
101
102//! [8]
103 Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
104 Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
105 Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
106 ...
107signals:
108 void colorChanged();
110 void textChanged(const QString &newText);
111
112private:
113 QColor m_color;
114 qreal m_spacing;
115 QString m_text;
116//! [8]
MyClass * myinstance
[5]
void spacingChanged()
int count
void textChanged(const QString &newText)
CallableWithState object