Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
38NSMenu *QMenu::toNSMenu()
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
55void QMenu::setAsDockMenu()
56{
57 Q_D(QMenu);
58 if (auto *cocoaPlatformMenu = dynamic_cast<QCocoaMenu *>(d->createPlatformMenu()))
59 cocoaPlatformMenu->setAsDockMenu();
60}
61
62void QMenuPrivate::moveWidgetToPlatformItem(QWidget *widget, QPlatformMenuItem* item)
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.
81 QWindow *widgetWindow = widget->windowHandle();
82 widgetWindow->create();
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.
90 widgetWindow->setFlag(Qt::SubWindow);
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.
96 item->setNativeContents(WId(widgetWindow->winId()));
97 widget->show();
98}
99
100#endif // QT_CONFIG(menu)
101
102#if QT_CONFIG(menubar)
103
113NSMenu *QMenuBar::toNSMenu()
114{
115 if (auto *cocoaMenuBar = dynamic_cast<QCocoaMenuBar *>(platformMenuBar()))
116 return cocoaMenuBar->nsMenu();
117
118 return nil;
119}
120#endif // QT_CONFIG(menubar)
121
123
QPlatformMenuBar * platformMenuBar()
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
void hide()
Hides the widget.
Definition qwidget.cpp:8135
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7875
QWindow * windowHandle() const
If this is a native widget, return the associated QWindow.
Definition qwidget.cpp:2483
\inmodule QtGui
Definition qwindow.h:63
QOpenGLWidget * widget
[1]
Combined button and popup list for selecting options.
@ WA_QuitOnClose
Definition qnamespace.h:342
@ WA_TranslucentBackground
Definition qnamespace.h:402
@ WA_NativeWindow
Definition qnamespace.h:378
@ SubWindow
Definition qnamespace.h:216
QGraphicsItem * item