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
qquicklabsplatformmenubar.cpp
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qquicklabsplatformmenubar_p.h"
6
#
include
"qquicklabsplatformmenu_p.h"
7
8
#
include
<
QtCore
/
qloggingcategory
.
h
>
9
#
include
<
QtGui
/
qpa
/
qplatformmenu
.
h
>
10
#
include
<
QtGui
/
qpa
/
qplatformtheme
.
h
>
11
#
include
<
QtGui
/
private
/
qguiapplication_p
.
h
>
12
#
include
<
QtQuick
/
qquickwindow
.
h
>
13
#
include
<
QtQuick
/
qquickitem
.
h
>
14
15
QT_BEGIN_NAMESPACE
16
17
/*!
18
\qmltype MenuBar
19
\inherits QtObject
20
\nativetype QQuickLabsPlatformMenuBar
21
\inqmlmodule Qt.labs.platform
22
\since 5.8
23
\brief A native menubar.
24
25
The MenuBar type provides a QML API for native platform menubars.
26
27
\image {qtlabsplatform-menubar.png} {A native menubar}
28
29
A menubar consists of a list of drop-down menus.
30
31
\code
32
MenuBar {
33
id: menuBar
34
35
Menu {
36
id: fileMenu
37
title: qsTr("File")
38
// ...
39
}
40
41
Menu {
42
id: editMenu
43
title: qsTr("&Edit")
44
// ...
45
}
46
47
Menu {
48
id: viewMenu
49
title: qsTr("&View")
50
// ...
51
}
52
53
Menu {
54
id: helpMenu
55
title: qsTr("&Help")
56
// ...
57
}
58
}
59
\endcode
60
61
MenuBar is currently available on the following platforms:
62
63
\list
64
\li macOS
65
\li Android
66
\li Linux (only available on desktop environments that provide a global D-Bus menu bar)
67
\li Windows
68
\endlist
69
70
\labs
71
72
\sa Menu
73
*/
74
75
QQuickLabsPlatformMenuBar
::
QQuickLabsPlatformMenuBar
(
QObject
*
parent
)
76
: QObject(
parent
),
77
m_complete(
false
),
78
m_window(
nullptr
),
79
m_handle(
nullptr
)
80
{
81
m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformMenuBar();
82
qCDebug(qtLabsPlatformMenus) <<
"MenuBar ->"
<< m_handle;
83
}
84
85
QQuickLabsPlatformMenuBar
::~
QQuickLabsPlatformMenuBar
()
86
{
87
for
(QQuickLabsPlatformMenu *menu : std::as_const(m_menus))
88
menu->setMenuBar(
nullptr
);
89
delete
m_handle;
90
m_handle =
nullptr
;
91
}
92
93
QPlatformMenuBar *
QQuickLabsPlatformMenuBar
::
handle
()
const
94
{
95
return
m_handle;
96
}
97
98
/*!
99
\qmldefault
100
\qmlproperty list<QtObject> Qt.labs.platform::MenuBar::data
101
102
This default property holds the list of all objects declared as children of
103
the menubar. The data property includes objects that are not \l Menu instances,
104
such as \l Timer and \l QtObject.
105
106
\sa menus
107
*/
108
QQmlListProperty
<
QObject
>
QQuickLabsPlatformMenuBar
::
data
()
109
{
110
return
QQmlListProperty<
QObject
>(
this
,
nullptr
, data_append, data_count, data_at, data_clear);
111
}
112
113
/*!
114
\qmlproperty list<Menu> Qt.labs.platform::MenuBar::menus
115
116
This property holds the list of menus in the menubar.
117
*/
118
QQmlListProperty
<
QQuickLabsPlatformMenu
>
QQuickLabsPlatformMenuBar
::
menus
()
119
{
120
return
QQmlListProperty<
QQuickLabsPlatformMenu
>(
this
,
nullptr
, menus_append, menus_count, menus_at, menus_clear);
121
}
122
123
/*!
124
\qmlproperty Window Qt.labs.platform::MenuBar::window
125
126
This property holds the menubar's window.
127
128
Unless explicitly set, the window is automatically resolved by iterating
129
the QML parent objects until a \l Window or an \l Item that has a window
130
is found.
131
*/
132
QWindow *
QQuickLabsPlatformMenuBar
::
window
()
const
133
{
134
return
m_window;
135
}
136
137
void
QQuickLabsPlatformMenuBar
::
setWindow
(QWindow *window)
138
{
139
if
(m_window == window)
140
return
;
141
142
if
(m_handle)
143
m_handle->handleReparent(window);
144
145
m_window = window;
146
emit windowChanged();
147
}
148
149
/*!
150
\qmlmethod void Qt.labs.platform::MenuBar::addMenu(Menu menu)
151
152
Adds a \a menu to end of the menubar.
153
*/
154
void
QQuickLabsPlatformMenuBar
::
addMenu
(
QQuickLabsPlatformMenu
*menu)
155
{
156
insertMenu(m_menus.size(), menu);
157
}
158
159
/*!
160
\qmlmethod void Qt.labs.platform::MenuBar::insertMenu(int index, Menu menu)
161
162
Inserts a \a menu at the specified \a index in the menubar.
163
*/
164
void
QQuickLabsPlatformMenuBar
::
insertMenu
(
int
index,
QQuickLabsPlatformMenu
*menu)
165
{
166
if
(!menu || m_menus.contains(menu))
167
return
;
168
169
QQuickLabsPlatformMenu
*before = m_menus.value(index);
170
m_menus.insert(index, menu);
171
m_data.append(menu);
172
menu
->
setMenuBar
(
this
)
;
173
if
(m_handle)
174
m_handle->insertMenu(menu
->
create
(
)
, before ? before
->
handle
(
)
:
nullptr
);
175
menu
->
sync
(
)
;
176
emit menusChanged();
177
}
178
179
/*!
180
\qmlmethod void Qt.labs.platform::MenuBar::removeMenu(Menu menu)
181
182
Removes a \a menu from the menubar.
183
*/
184
void
QQuickLabsPlatformMenuBar
::
removeMenu
(
QQuickLabsPlatformMenu
*menu)
185
{
186
if
(!menu || !m_menus.removeOne(menu))
187
return
;
188
189
m_data.removeOne(menu);
190
if
(m_handle)
191
m_handle->removeMenu(menu
->
handle
(
)
);
192
menu
->
setMenuBar
(
nullptr
)
;
193
emit menusChanged();
194
}
195
196
/*!
197
\qmlmethod void Qt.labs.platform::MenuBar::clear()
198
199
Removes all menus from the menubar.
200
*/
201
void
QQuickLabsPlatformMenuBar
::
clear
()
202
{
203
if
(m_menus.isEmpty())
204
return
;
205
206
for
(QQuickLabsPlatformMenu *menu : std::as_const(m_menus)) {
207
m_data.removeOne(menu);
208
if
(m_handle)
209
m_handle->removeMenu(menu->handle());
210
menu->setMenuBar(
nullptr
);
211
delete
menu;
212
}
213
214
m_menus.clear();
215
emit menusChanged();
216
}
217
218
void
QQuickLabsPlatformMenuBar
::
classBegin
()
219
{
220
}
221
222
void
QQuickLabsPlatformMenuBar
::
componentComplete
()
223
{
224
m_complete =
true
;
225
for
(QQuickLabsPlatformMenu *menu : std::as_const(m_menus))
226
menu->sync();
227
if
(!m_window)
228
setWindow
(
findWindow
(
)
)
;
229
}
230
231
QWindow *
QQuickLabsPlatformMenuBar
::
findWindow
()
const
232
{
233
QObject *obj = parent();
234
while
(obj) {
235
QWindow *window = qobject_cast<QWindow *>(obj);
236
if
(window)
237
return
window;
238
QQuickItem *item = qobject_cast<QQuickItem *>(obj);
239
if
(item && item->window())
240
return
item->window();
241
obj = obj->parent();
242
}
243
return
nullptr
;
244
}
245
246
void
QQuickLabsPlatformMenuBar
::data_append(QQmlListProperty<QObject> *property, QObject *object)
247
{
248
QQuickLabsPlatformMenuBar *menuBar =
static_cast
<QQuickLabsPlatformMenuBar *>(property->object);
249
QQuickLabsPlatformMenu *menu = qobject_cast<QQuickLabsPlatformMenu *>(object);
250
if
(menu)
251
menuBar->addMenu(menu);
252
else
253
menuBar->m_data.append(object);
254
}
255
256
qsizetype
QQuickLabsPlatformMenuBar
::data_count(QQmlListProperty<QObject> *property)
257
{
258
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
259
return
menuBar->m_data.size();
260
}
261
262
QObject *
QQuickLabsPlatformMenuBar
::data_at(QQmlListProperty<QObject> *property, qsizetype index)
263
{
264
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
265
return
menuBar->m_data.value(index);
266
}
267
268
void
QQuickLabsPlatformMenuBar
::data_clear(QQmlListProperty<QObject> *property)
269
{
270
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
271
menuBar->m_data.clear();
272
}
273
274
void
QQuickLabsPlatformMenuBar
::menus_append(QQmlListProperty<QQuickLabsPlatformMenu> *property,
QQuickLabsPlatformMenu
*menu)
275
{
276
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
277
menuBar->addMenu(menu);
278
}
279
280
qsizetype
QQuickLabsPlatformMenuBar
::menus_count(QQmlListProperty<QQuickLabsPlatformMenu> *property)
281
{
282
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
283
return
menuBar->m_menus.size();
284
}
285
286
QQuickLabsPlatformMenu
*
QQuickLabsPlatformMenuBar
::menus_at(QQmlListProperty<QQuickLabsPlatformMenu> *property, qsizetype index)
287
{
288
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
289
return
menuBar->m_menus.value(index);
290
}
291
292
void
QQuickLabsPlatformMenuBar
::menus_clear(QQmlListProperty<QQuickLabsPlatformMenu> *property)
293
{
294
QQuickLabsPlatformMenuBar
*menuBar =
static_cast
<
QQuickLabsPlatformMenuBar
*>(property->object);
295
menuBar->clear();
296
}
297
298
QT_END_NAMESPACE
299
300
#
include
"moc_qquicklabsplatformmenubar_p.cpp"
QObjectData::parent
QObject * parent
Definition
qobject.h:74
QObject
\inmodule QtCore
Definition
qobject.h:106
QQuickLabsPlatformMenuBar
Definition
qquicklabsplatformmenubar_p.h:32
QQuickLabsPlatformMenuBar::~QQuickLabsPlatformMenuBar
~QQuickLabsPlatformMenuBar()
Definition
qquicklabsplatformmenubar.cpp:85
QQuickLabsPlatformMenuBar::data
QQmlListProperty< QObject > data()
QQuickLabsPlatformMenuBar::removeMenu
Q_INVOKABLE void removeMenu(QQuickLabsPlatformMenu *menu)
\qmlmethod void Qt.labs.platform::MenuBar::removeMenu(Menu menu)
Definition
qquicklabsplatformmenubar.cpp:184
QQuickLabsPlatformMenuBar::setWindow
void setWindow(QWindow *window)
Definition
qquicklabsplatformmenubar.cpp:137
QQuickLabsPlatformMenuBar::findWindow
QWindow * findWindow() const
Definition
qquicklabsplatformmenubar.cpp:231
QQuickLabsPlatformMenuBar::clear
Q_INVOKABLE void clear()
\qmlmethod void Qt.labs.platform::MenuBar::clear()
Definition
qquicklabsplatformmenubar.cpp:201
QQuickLabsPlatformMenuBar::classBegin
void classBegin() override
Invoked after class creation, but before any properties have been set.
Definition
qquicklabsplatformmenubar.cpp:218
QQuickLabsPlatformMenuBar::window
QWindow * window() const
\qmlproperty Window Qt.labs.platform::MenuBar::window
Definition
qquicklabsplatformmenubar.cpp:132
QQuickLabsPlatformMenuBar::insertMenu
Q_INVOKABLE void insertMenu(int index, QQuickLabsPlatformMenu *menu)
\qmlmethod void Qt.labs.platform::MenuBar::insertMenu(int index, Menu menu)
Definition
qquicklabsplatformmenubar.cpp:164
QQuickLabsPlatformMenuBar::componentComplete
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
Definition
qquicklabsplatformmenubar.cpp:222
QQuickLabsPlatformMenuBar::menus
QQmlListProperty< QQuickLabsPlatformMenu > menus()
QQuickLabsPlatformMenuBar::addMenu
Q_INVOKABLE void addMenu(QQuickLabsPlatformMenu *menu)
\qmlmethod void Qt.labs.platform::MenuBar::addMenu(Menu menu)
Definition
qquicklabsplatformmenubar.cpp:154
QQuickLabsPlatformMenuBar::handle
QPlatformMenuBar * handle() const
Definition
qquicklabsplatformmenubar.cpp:93
QQuickLabsPlatformMenu
Definition
qquicklabsplatformmenu_p.h:44
QQuickLabsPlatformMenu::sync
void sync()
Definition
qquicklabsplatformmenu.cpp:233
QQuickLabsPlatformMenu::create
QPlatformMenu * create()
Definition
qquicklabsplatformmenu.cpp:180
QQuickLabsPlatformMenu::handle
QPlatformMenu * handle() const
Definition
qquicklabsplatformmenu.cpp:175
QQuickLabsPlatformMenu::setMenuBar
void setMenuBar(QQuickLabsPlatformMenuBar *menuBar)
Definition
qquicklabsplatformmenu.cpp:293
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
labs
platform
qquicklabsplatformmenubar.cpp
Generated on
for Qt by
1.18.0