aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickmenu.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-06-01 20:09:44 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-06-02 14:05:45 +0000
commitecef73ddb0c6038f6e0208121cfb8786c2cab7f2 (patch)
tree7d5834b7afdd8eb635e6a301b6d670a3938ee4f2 /src/quicktemplates2/qquickmenu.cpp
parent1b98cac7c0f7d5515f7bcc6e55e8291eff9e43b7 (diff)
QQuickMenu: add support for declaring Actions
[ChangeLog][Controls][Menu] Added support for declaring Actions. The new "delegate" property is used to define a Component that is used to create menu items that present the actions. Change-Id: If26f38f6910aa5592879703429a2b418193d5710 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickmenu.cpp')
-rw-r--r--src/quicktemplates2/qquickmenu.cpp80
1 files changed, 79 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp
index 726a0fca..63d5d00c 100644
--- a/src/quicktemplates2/qquickmenu.cpp
+++ b/src/quicktemplates2/qquickmenu.cpp
@@ -39,11 +39,14 @@
#include "qquickmenuitem_p.h"
#include "qquickcontrol_p_p.h"
#include "qquickpopupitem_p_p.h"
+#include "qquickaction_p.h"
#include <QtGui/qevent.h>
#include <QtGui/qcursor.h>
#include <QtGui/qpa/qplatformintegration.h>
#include <QtGui/private/qguiapplication_p.h>
+#include <QtQml/qqmlcontext.h>
+#include <QtQml/qqmlcomponent.h>
#include <QtQml/private/qqmlengine_p.h>
#include <QtQml/private/qv4scopedvalue_p.h>
#include <QtQml/private/qv4variantobject_p.h>
@@ -129,6 +132,17 @@ QT_BEGIN_NAMESPACE
}
\endcode
+ Since QtQuick.Controls 2.3 (Qt 5.10), it is also possible to declare
+ Action objects inside Menu:
+
+ \code
+ Menu {
+ Action { text: "Cut" }
+ Action { text: "Copy" }
+ Action { text: "Paste" }
+ }
+ \endcode
+
Typically, menu items are statically declared as children of the menu, but
Menu also provides API to \l {addItem}{add}, \l {insertItem}{insert},
\l {moveItem}{move} and \l {removeItem}{remove} items dynamically. The
@@ -143,7 +157,8 @@ QT_BEGIN_NAMESPACE
QQuickMenuPrivate::QQuickMenuPrivate()
: contentItem(nullptr),
- contentModel(nullptr)
+ contentModel(nullptr),
+ delegate(nullptr)
{
Q_Q(QQuickMenu);
contentModel = new QQmlObjectModel(q);
@@ -196,6 +211,30 @@ void QQuickMenuPrivate::removeItem(int index, QQuickItem *item)
}
}
+QQuickItem *QQuickMenuPrivate::createItem(QQuickAction *action)
+{
+ Q_Q(QQuickMenu);
+ if (!delegate)
+ return nullptr;
+
+ QQmlContext *creationContext = delegate->creationContext();
+ if (!creationContext)
+ creationContext = qmlContext(q);
+ QQmlContext *context = new QQmlContext(creationContext, q);
+ context->setContextObject(q);
+
+ QObject *object = delegate->beginCreate(context);
+ if (QQuickItem *item = qobject_cast<QQuickItem *>(object)) {
+ if (QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(object))
+ button->setAction(action);
+ delegate->completeCreate();
+ return item;
+ }
+
+ delete object;
+ return nullptr;
+}
+
void QQuickMenuPrivate::resizeItem(QQuickItem *item)
{
if (!item || !contentItem)
@@ -293,7 +332,13 @@ void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObj
{
QQuickMenuPrivate *p = static_cast<QQuickMenuPrivate *>(prop->data);
QQuickMenu *q = static_cast<QQuickMenu *>(prop->object);
+
QQuickItem *item = qobject_cast<QQuickItem *>(obj);
+ if (!item) {
+ if (QQuickAction *action = qobject_cast<QQuickAction *>(obj))
+ item = p->createItem(action);
+ }
+
if (item) {
if (QQuickItemPrivate::get(item)->isTransparentForPositioner()) {
QQuickItemPrivate::get(item)->addItemChangeListener(p, QQuickItemPrivate::SiblingOrder);
@@ -493,6 +538,39 @@ void QQuickMenu::setTitle(QString &title)
/*!
\since QtQuick.Controls 2.3 (Qt 5.10)
+ \qmlproperty Component QtQuick.Controls::Menu::delegate
+
+ This property holds the component that is used to create items
+ to present actions.
+
+ \code
+ Menu {
+ Action { text: "Cut" }
+ Action { text: "Copy" }
+ Action { text: "Paste" }
+ }
+ \endcode
+
+ \sa Action
+*/
+QQmlComponent *QQuickMenu::delegate() const
+{
+ Q_D(const QQuickMenu);
+ return d->delegate;
+}
+
+void QQuickMenu::setDelegate(QQmlComponent *delegate)
+{
+ Q_D(QQuickMenu);
+ if (d->delegate == delegate)
+ return;
+
+ d->delegate = delegate;
+ emit delegateChanged();
+}
+
+/*!
+ \since QtQuick.Controls 2.3 (Qt 5.10)
\qmlmethod void QtQuick.Controls::Menu::popup(MenuItem item = null)
Opens the menu at the mouse cursor on desktop platforms that have a mouse cursor