diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-03-07 13:28:10 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-03-08 10:45:08 +0000 |
commit | e6eaf29c69f0f352fc8279b9f508ae468d18cafd (patch) | |
tree | 2ec01eda5bdb29387cc7264327b7034d4f704051 /src/imports | |
parent | 8732b0681b99e5de01d6f53de33306ca70b01171 (diff) |
Platform Menus: support sub menus in the Qt Widgets fallback menus
Implement all the missing things and fix the type of the cast in
QWidgetPlatformMenuItem::setMenu() to make sub menus work with the
widgets fallbacks.
Change-Id: I19bb010a5c639a77b76d1d92a47ad03576c06d2f
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports')
4 files changed, 26 insertions, 13 deletions
diff --git a/src/imports/platform/widgets/qwidgetplatformmenu.cpp b/src/imports/platform/widgets/qwidgetplatformmenu.cpp index eddc7ccc..1aacb0ff 100644 --- a/src/imports/platform/widgets/qwidgetplatformmenu.cpp +++ b/src/imports/platform/widgets/qwidgetplatformmenu.cpp @@ -44,7 +44,7 @@ QT_BEGIN_NAMESPACE QWidgetPlatformMenu::QWidgetPlatformMenu(QObject *parent) - : m_menu(new QMenu) + : m_tag(reinterpret_cast<quintptr>(this)), m_menu(new QMenu) { setParent(parent); @@ -69,6 +69,10 @@ void QWidgetPlatformMenu::insertMenuItem(QPlatformMenuItem *item, QPlatformMenuI QWidgetPlatformMenuItem *widgetBefore = qobject_cast<QWidgetPlatformMenuItem *>(before); m_menu->insertAction(widgetBefore ? widgetBefore->action() : nullptr, widgetItem->action()); + int index = m_items.indexOf(widgetBefore); + if (index < 0) + index = m_items.count(); + m_items.insert(index, widgetItem); } void QWidgetPlatformMenu::removeMenuItem(QPlatformMenuItem *item) @@ -77,6 +81,7 @@ void QWidgetPlatformMenu::removeMenuItem(QPlatformMenuItem *item) if (!widgetItem) return; + m_items.removeOne(widgetItem); m_menu->removeAction(widgetItem->action()); } @@ -87,17 +92,17 @@ void QWidgetPlatformMenu::syncMenuItem(QPlatformMenuItem *item) void QWidgetPlatformMenu::syncSeparatorsCollapsible(bool enable) { - Q_UNUSED(enable); + m_menu->setSeparatorsCollapsible(enable); } quintptr QWidgetPlatformMenu::tag() const { - return 0; + return m_tag; } void QWidgetPlatformMenu::setTag(quintptr tag) { - Q_UNUSED(tag); + m_tag = tag; } void QWidgetPlatformMenu::setText(const QString &text) @@ -163,24 +168,26 @@ void QWidgetPlatformMenu::dismiss() QPlatformMenuItem *QWidgetPlatformMenu::menuItemAt(int position) const { - Q_UNUSED(position); - return nullptr; + return m_items.value(position); } QPlatformMenuItem *QWidgetPlatformMenu::menuItemForTag(quintptr tag) const { - Q_UNUSED(tag); + for (QWidgetPlatformMenuItem *item : m_items) { + if (item->tag() == tag) + return item; + } return nullptr; } QPlatformMenuItem *QWidgetPlatformMenu::createMenuItem() const { - return nullptr; + return new QWidgetPlatformMenuItem; } QPlatformMenu *QWidgetPlatformMenu::createSubMenu() const { - return nullptr; + return new QWidgetPlatformMenu; } QT_END_NAMESPACE diff --git a/src/imports/platform/widgets/qwidgetplatformmenu_p.h b/src/imports/platform/widgets/qwidgetplatformmenu_p.h index 2eea6470..1df9ef78 100644 --- a/src/imports/platform/widgets/qwidgetplatformmenu_p.h +++ b/src/imports/platform/widgets/qwidgetplatformmenu_p.h @@ -53,6 +53,7 @@ QT_BEGIN_NAMESPACE class QMenu; +class QWidgetPlatformMenuItem; class QWidgetPlatformMenu : public QPlatformMenu { @@ -91,7 +92,9 @@ public: QPlatformMenu *createSubMenu() const override; private: + quintptr m_tag; QScopedPointer<QMenu> m_menu; + QVector<QWidgetPlatformMenuItem *> m_items; }; QT_END_NAMESPACE diff --git a/src/imports/platform/widgets/qwidgetplatformmenuitem.cpp b/src/imports/platform/widgets/qwidgetplatformmenuitem.cpp index c7576296..c44ede9f 100644 --- a/src/imports/platform/widgets/qwidgetplatformmenuitem.cpp +++ b/src/imports/platform/widgets/qwidgetplatformmenuitem.cpp @@ -35,6 +35,7 @@ ****************************************************************************/ #include "qwidgetplatformmenuitem_p.h" +#include "qwidgetplatformmenu_p.h" #include <QtWidgets/qmenu.h> #include <QtWidgets/qaction.h> @@ -42,7 +43,7 @@ QT_BEGIN_NAMESPACE QWidgetPlatformMenuItem::QWidgetPlatformMenuItem(QObject *parent) - : m_action(new QAction) + : m_tag(reinterpret_cast<quintptr>(this)), m_action(new QAction) { setParent(parent); connect(m_action.data(), &QAction::hovered, this, &QPlatformMenuItem::hovered); @@ -60,12 +61,12 @@ QAction *QWidgetPlatformMenuItem::action() const quintptr QWidgetPlatformMenuItem::tag() const { - return 0; + return m_tag; } void QWidgetPlatformMenuItem::setTag(quintptr tag) { - Q_UNUSED(tag); + m_tag = tag; } void QWidgetPlatformMenuItem::setText(const QString &text) @@ -80,7 +81,8 @@ void QWidgetPlatformMenuItem::setIcon(const QIcon &icon) void QWidgetPlatformMenuItem::setMenu(QPlatformMenu *menu) { - m_action->setMenu(qobject_cast<QMenu *>(menu)); + QWidgetPlatformMenu *widgetMenu = qobject_cast<QWidgetPlatformMenu *>(menu); + m_action->setMenu(widgetMenu ? widgetMenu->menu() : nullptr); } void QWidgetPlatformMenuItem::setVisible(bool visible) diff --git a/src/imports/platform/widgets/qwidgetplatformmenuitem_p.h b/src/imports/platform/widgets/qwidgetplatformmenuitem_p.h index 82398bb8..2f69ece8 100644 --- a/src/imports/platform/widgets/qwidgetplatformmenuitem_p.h +++ b/src/imports/platform/widgets/qwidgetplatformmenuitem_p.h @@ -81,6 +81,7 @@ public: void setIconSize(int size) override; private: + quintptr m_tag; QScopedPointer<QAction> m_action; }; |