Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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]
6
Q_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]
26
Q_PROPERTY(
bool
focus READ hasFocus)
27
Q_PROPERTY(
bool
enabled READ isEnabled WRITE setEnabled)
28
Q_PROPERTY(QCursor cursor READ cursor WRITE setCursor RESET unsetCursor)
29
//! [1]
30
31
//! [2]
32
Q_PROPERTY(QDate date READ getDate WRITE setDate)
33
//! [2]
34
#
endif
35
36
#
if
__has_include
(
<
QPushButton
>
)
37
#
include
<
QPushButton
>
38
void
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
>
51
void
qobject_example
()
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]
67
class
MyClass
:
public
QObject
68
{
69
Q_OBJECT
70
Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
71
72
public
:
73
MyClass
(
QObject
*
parent
=
nullptr
);
74
~
MyClass
();
75
76
enum
Priority
{
High
,
Low
,
VeryHigh
,
VeryLow
};
77
Q_ENUM
(
Priority
)
78
79
void
setPriority
(
Priority
priority)
80
{
81
if
(m_priority == priority)
82
return
;
83
84
m_priority = priority;
85
emit priorityChanged(priority);
86
}
87
Priority
priority
()
const
88
{
return
m_priority; }
89
90
signals
:
91
void
priorityChanged
(
Priority
);
92
93
private
:
94
Priority
m_priority;
95
};
96
//! [5]
97
98
void
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]
110
Q_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
//...
118
signals:
119
void
colorChanged();
120
void
spacingChanged();
121
void
textChanged(
const
QString &newText);
122
123
private
:
124
QColor m_color;
125
qreal m_spacing;
126
QString m_text;
127
//! [8]
128
#
endif
MyClass
[5]
Definition
doc_src_properties.cpp:68
MyClass::priority
Priority priority() const
Definition
doc_src_properties.cpp:87
MyClass::setPriority
void setPriority(Priority priority)
Definition
doc_src_properties.cpp:79
MyClass::Priority
Priority
Definition
doc_src_properties.cpp:76
MyClass::VeryHigh
@ VeryHigh
Definition
doc_src_properties.cpp:76
MyClass::High
@ High
Definition
doc_src_properties.cpp:76
MyClass::Low
@ Low
Definition
doc_src_properties.cpp:76
MyClass::VeryLow
@ VeryLow
Definition
doc_src_properties.cpp:76
MyClass::~MyClass
~MyClass()
example
void example()
[5]
Definition
doc_src_groups.cpp:7
qobject_example
void qobject_example()
Definition
doc_src_properties.cpp:51
__has_include
#define __has_include(x)
Definition
qcompilerdetection.h:459
qtbase
src
corelib
doc
snippets
code
doc_src_properties.cpp
Generated on
for Qt by
1.18.0