aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/macextras/qmacfunctions.h2
-rw-r--r--src/macextras/qmacfunctions.mm15
-rw-r--r--tests/auto/macfunctions/tst_qmacfunctions.mm12
3 files changed, 27 insertions, 2 deletions
diff --git a/src/macextras/qmacfunctions.h b/src/macextras/qmacfunctions.h
index 7b3bd51..e2dc048 100644
--- a/src/macextras/qmacfunctions.h
+++ b/src/macextras/qmacfunctions.h
@@ -64,6 +64,7 @@ typedef struct objc_object NSMenu;
QT_BEGIN_NAMESPACE
class QMenu;
+class QMenuBar;
class QPixmap;
class QString;
@@ -80,6 +81,7 @@ Q_MACEXTRAS_EXPORT QPixmap fromMacCGImageRef(CGImageRef image);
#ifndef Q_OS_IOS
Q_MACEXTRAS_EXPORT NSMenu* toNSMenu(QMenu *menu);
+Q_MACEXTRAS_EXPORT NSMenu *toNSMenu(QMenuBar *menubar);
Q_MACEXTRAS_EXPORT NSImage* toMacNSImage(const QPixmap &pixmap);
Q_MACEXTRAS_EXPORT NSImage* toMacNSImage(const QPixmap &pixmap);
#endif
diff --git a/src/macextras/qmacfunctions.mm b/src/macextras/qmacfunctions.mm
index e2ba0b0..3065b6f 100644
--- a/src/macextras/qmacfunctions.mm
+++ b/src/macextras/qmacfunctions.mm
@@ -53,6 +53,7 @@ QT_BEGIN_NAMESPACE
#include <QtCore/qDebug.h>
#include <QtGui/QGuiApplication>
#include <QtWidgets/QMenu>
+#include <QtWidgets/QMenuBar>
#include <qpa/qplatformmenu.h>
#include <qpa/qplatformnativeinterface.h>
@@ -96,6 +97,20 @@ NSMenu *QtMacExtras::toNSMenu(QMenu *menu)
}
#endif
+NSMenu *QtMacExtras::toNSMenu(QMenuBar *menubar)
+{
+ // Get the platform menubar, which will be a QCocoaMenuBar
+ QPlatformMenuBar *platformMenuBar = menubar->platformMenuBar();
+
+ // Get the qMenuBarToNSMenu function and call it.
+ QPlatformNativeInterface::NativeResourceForIntegrationFunction function = resolvePlatformFunction("qmenubartonsmenu");
+ if (function) {
+ typedef void* (*QMenuBarToNSMenuFunction)(QPlatformMenuBar *platformMenuBar);
+ return reinterpret_cast<NSMenu *>(reinterpret_cast<QMenuBarToNSMenuFunction>(function)(platformMenuBar));
+ }
+ return nil;
+}
+
#endif
namespace QtMacExtras
diff --git a/tests/auto/macfunctions/tst_qmacfunctions.mm b/tests/auto/macfunctions/tst_qmacfunctions.mm
index 12b9327..e7a36e5 100644
--- a/tests/auto/macfunctions/tst_qmacfunctions.mm
+++ b/tests/auto/macfunctions/tst_qmacfunctions.mm
@@ -57,14 +57,14 @@ public:
tst_QMacFunctions();
private slots:
- void testQMenuToNSMenu();
+ void testToNSMenu();
};
tst_QMacFunctions::tst_QMacFunctions()
{
}
-void tst_QMacFunctions::testQMenuToNSMenu()
+void tst_QMacFunctions::testToNSMenu()
{
QMainWindow window;
QMenu *qMenu = new QMenu("Menu", &window);
@@ -78,6 +78,14 @@ void tst_QMacFunctions::testQMenuToNSMenu()
NSMenuItem *item = [nsMenu itemAtIndex:0];
QCOMPARE([[item title] UTF8String], "Item");
+
+ // get NSMenu from QMenuBar
+ nsMenu = QtMacExtras::toNSMenu(window.menuBar());
+ QVERIFY(nsMenu != NULL);
+
+ // the first item should be our menu
+ item = [nsMenu itemAtIndex:0];
+ QCOMPARE([[item title] UTF8String], "Menu");
}
QTEST_MAIN(tst_QMacFunctions)