aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates/qquickmenu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quicktemplates/qquickmenu.cpp')
-rw-r--r--src/quicktemplates/qquickmenu.cpp43
1 files changed, 33 insertions, 10 deletions
diff --git a/src/quicktemplates/qquickmenu.cpp b/src/quicktemplates/qquickmenu.cpp
index ea37af1885..707b115983 100644
--- a/src/quicktemplates/qquickmenu.cpp
+++ b/src/quicktemplates/qquickmenu.cpp
@@ -4,8 +4,11 @@
#include "qquickmenu_p.h"
#include "qquickmenu_p_p.h"
#include "qquickmenuitem_p_p.h"
+#include <private/qtquicktemplates2-config_p.h>
+#if QT_CONFIG(quicktemplates2_container)
#include "qquickmenubaritem_p.h"
#include "qquickmenubar_p.h"
+#endif
#include "qquickpopupitem_p_p.h"
#include "qquickpopuppositioner_p_p.h"
#include "qquickaction_p.h"
@@ -26,7 +29,6 @@
#include <private/qqmlobjectmodel_p.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickitemchangelistener_p.h>
-#include <QtQuick/private/qquickitemview_p.h>
#include <QtQuick/private/qquickevents_p_p.h>
#include <QtQuick/private/qquickwindow_p.h>
@@ -107,6 +109,16 @@ static const int SUBMENU_DELAY = 225;
}
\endcode
+ If the button should also close the menu when clicked, use the
+ \c Popup.CloseOnPressOutsideParent flag:
+ \code
+ onClicked: menu.visible = !menu.visible
+
+ Menu {
+ // ...
+ closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
+ \endcode
+
Since QtQuick.Controls 2.3 (Qt 5.10), it is also possible to create sub-menus
and declare Action objects inside Menu:
@@ -571,11 +583,13 @@ void QQuickMenuPrivate::propagateKeyEvent(QKeyEvent *event)
if (QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(parentItem)) {
if (QQuickMenu *menu = menuItem->menu())
QQuickMenuPrivate::get(menu)->propagateKeyEvent(event);
+#if QT_CONFIG(quicktemplates2_container)
} else if (QQuickMenuBarItem *menuBarItem = qobject_cast<QQuickMenuBarItem *>(parentItem)) {
if (QQuickMenuBar *menuBar = menuBarItem->menuBar()) {
event->accept();
QCoreApplication::sendEvent(menuBar, event);
}
+#endif
}
}
@@ -732,6 +746,15 @@ QQuickMenu::~QQuickMenu()
// been destroyed before that is called.
while (d->contentModel->count() > 0)
d->removeItem(0, d->itemAt(0));
+
+ if (d->contentItem) {
+ QQuickItemPrivate::get(d->contentItem)->removeItemChangeListener(d, QQuickItemPrivate::Children);
+ QQuickItemPrivate::get(d->contentItem)->removeItemChangeListener(d, QQuickItemPrivate::Geometry);
+
+ const auto children = d->contentItem->childItems();
+ for (QQuickItem *child : std::as_const(children))
+ QQuickItemPrivate::get(child)->removeItemChangeListener(d, QQuickItemPrivate::SiblingOrder);
+ }
}
/*!
@@ -1095,7 +1118,7 @@ QString QQuickMenu::title() const
return d->title;
}
-void QQuickMenu::setTitle(QString &title)
+void QQuickMenu::setTitle(const QString &title)
{
Q_D(QQuickMenu);
if (title == d->title)
@@ -1116,7 +1139,7 @@ void QQuickMenu::setTitle(QString &title)
\include qquickicon.qdocinc grouped-properties
- \sa text, display, {Icons in Qt Quick Controls}
+ \sa AbstractButton::text, AbstractButton::display, {Icons in Qt Quick Controls}
*/
QQuickIcon QQuickMenu::icon() const
@@ -1284,10 +1307,10 @@ void QQuickMenu::popup(QQuickItem *menuItem)
#endif
// As a fallback, center the menu over its parent item.
- if (pos.isNull && d->parentItem)
+ if (!pos.isValid() && d->parentItem)
pos = QPointF((d->parentItem->width() - width()) / 2, (d->parentItem->height() - height()) / 2);
- popup(pos.isNull ? QPointF() : pos.value, menuItem);
+ popup(pos.isValid() ? pos.value() : QPointF(), menuItem);
}
void QQuickMenu::popup(const QPointF &pos, QQuickItem *menuItem)
@@ -1343,7 +1366,7 @@ void QQuickMenu::popup(const QPointF &pos, QQuickItem *menuItem)
\sa dismiss(), Popup::open()
*/
-void QQuickMenu::popup(QQmlV4Function *args)
+void QQuickMenu::popup(QQmlV4FunctionPtr args)
{
Q_D(QQuickMenu);
const int len = args->length();
@@ -1388,7 +1411,7 @@ void QQuickMenu::popup(QQmlV4Function *args)
pos = QPointF(xArg->asDouble(), yArg->asDouble());
}
- if (pos.isNull && (len >= 2 || (!parentItem && len >= 1))) {
+ if (!pos.isValid() && (len >= 2 || (!parentItem && len >= 1))) {
// point pos
QV4::ScopedValue posArg(scope, (*args)[parentItem ? 1 : 0]);
const QVariant var = QV4::ExecutionEngine::toVariant(posArg, QMetaType {});
@@ -1399,10 +1422,10 @@ void QQuickMenu::popup(QQmlV4Function *args)
if (parentItem)
setParentItem(parentItem);
- if (pos.isNull)
- popup(menuItem);
- else
+ if (pos.isValid())
popup(pos, menuItem);
+ else
+ popup(menuItem);
}
/*!