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