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
qmenu_mac.mm
Go to the documentation of this file.
1// Copyright (C) 2016 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#import <Foundation/Foundation.h>
6#import <AppKit/AppKit.h>
7
8#include <qtwidgetsglobal.h>
9
10QT_USE_NAMESPACE
11
12#include "qmenu.h"
13#if QT_CONFIG(menubar)
14#include "qmenubar.h"
15#include "qmenubar_p.h"
16#endif
17
18#include <QtCore/QDebug>
19#include <QtGui/QGuiApplication>
20#include <QtGui/QWindow>
21#include <qpa/qplatformnativeinterface.h>
22#include <qpa/qplatformmenu_p.h>
23
24using namespace QNativeInterface::Private;
25
27
28#if QT_CONFIG(menu)
29
30/*!
31 \fn NSMenu *QMenu::toNSMenu()
32 \since 5.2
33
34 Returns the native NSMenu for this menu. Available on \macos only.
35
36 \note Qt sets the delegate on the native menu. If you need to set your own
37 delegate, make sure you save the original one and forward any calls to it.
38*/
40{
41 Q_D(QMenu);
42 if (auto *cocoaPlatformMenu = dynamic_cast<QCocoaMenu *>(d->createPlatformMenu()))
43 return cocoaPlatformMenu->nsMenu();
44
45 return nil;
46}
47
48
49/*!
50 \fn void QMenu::setAsDockMenu()
51 \since 5.2
52
53 Set this menu to be the dock menu available by option-clicking
54 on the application dock icon. Available on \macos only.
55*/
56void QMenu::setAsDockMenu()
57{
58 Q_D(QMenu);
59 if (auto *cocoaPlatformMenu = dynamic_cast<QCocoaMenu *>(d->createPlatformMenu()))
61}
62
64{
65 // Hide the widget before we mess with it
66 widget->hide();
67
68 // Move out of QMenu, since this widget will live in the native menu item
69 widget->setParent(nullptr);
70
71 // Make sure the widget doesn't prevent quitting the application,
72 // just because it's a parent-less (top level) window.
74
75 // And that it blends nicely with the native menu background
77
78 // Trigger creation of the backing QWindow, the platform window, and its
79 // underlying NSView and NSWindow. At this point the widget is still hidden,
80 // so the corresponding NSWindow that is created is not shown.
84
85 // Inform the window that it's actually a sub-window. This
86 // ensures that we dispose of the NSWindow when the widget is
87 // finally shown. We need to do this on a QWindow level, as
88 // QWidget will ignore the flag if there is no parentWidget().
89 // And we need to do it after creating the platform window, as
90 // QWidget will overwrite the window flags during creation.
92
93 // Finally, we can associate the underlying NSView with the menu item,
94 // and show it. This will dispose of the created NSWindow, due to
95 // the Qt::SubWindow flag above. The widget will not actually be
96 // visible until it's re-parented into the NSMenu hierarchy.
98 widget->show();
99}
100
101#endif // QT_CONFIG(menu)
102
103#if QT_CONFIG(menubar)
104
105/*!
106 \fn NSMenu *QMenuBar::toNSMenu()
107 \since 5.2
108
109 Returns the native NSMenu for this menu bar. Available on \macos only.
110
111 \note Qt may set the delegate on the native menu bar. If you need to set your
112 own delegate, make sure you save the original one and forward any calls to it.
113*/
115{
116 if (auto *cocoaMenuBar = dynamic_cast<QCocoaMenuBar *>(platformMenuBar()))
117 return cocoaMenuBar->nsMenu();
118
119 return nil;
120}
121#endif // QT_CONFIG(menubar)
122
\inmodule QtSql