diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-06-02 16:18:56 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-06-06 11:16:19 +0000 |
commit | 841e4355d394af0d29ca4dd64e6fb45f4d627189 (patch) | |
tree | 6a0d4131c335efdae2720b6801c1eec780d874bb /src/quicktemplates2/qquickmenu.cpp | |
parent | 00594b6e0554950ba3f8683f452c1ca2574f80c5 (diff) |
QQuickMenu: removeItem(Item) and takeItem(int)
Deprecate removeItem(int) in favor of removeItem(Item) and
takeItem(int) with clearer semantics.
If one already has access to an item, one can pass it to removeItem()
to get rid of it for good, instead of having to call destroy() by hand.
If one knows the index instead, we provide a way to take the respective
item out of the menu, and let the user decide what to do with it. This
way we provide convenient ways to remove & destroy items in one go, and
to take items out without destroying them.
This nice pattern was once upon time planned for the QQC1 containers
such as SplitView, and should have been used for the QQC2 containers,
but was simply forgotten during the development...
[ChangeLog][Controls][Menu] Deprecated removeItem(int) in favor of
removeItem(Item) and takeItem(int) with clearer semantics. The former
destroys the item, whereas the latter transfers ownership to the
caller.
Change-Id: Id2a07fb65019b2904a013c8bcc3154089d36a0ea
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickmenu.cpp')
-rw-r--r-- | src/quicktemplates2/qquickmenu.cpp | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 67725160..c7dd0ea5 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -443,22 +443,61 @@ void QQuickMenu::moveItem(int from, int to) } /*! + \deprecated \qmlmethod void QtQuick.Controls::Menu::removeItem(int index) - Removes the item at \a index. + Use Menu::removeItem(Item) or Menu::takeItem(int) instead. +*/ +void QQuickMenu::removeItem(const QVariant &var) +{ + if (var.userType() == QMetaType::Nullptr) + return; + + if (QQuickItem *item = var.value<QQuickItem *>()) + removeItem(item); + else + takeItem(var.toInt()); +} + +/*! + \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlmethod void QtQuick.Controls::Menu::removeItem(Item item) + + Removes and destroys the specified \a item. +*/ +void QQuickMenu::removeItem(QQuickItem *item) +{ + Q_D(QQuickMenu); + if (!item) + return; + + const int index = d->contentModel->indexOf(item, nullptr); + if (index == -1) + return; + + d->removeItem(index, item); + item->deleteLater(); +} + +/*! + \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlmethod MenuItem QtQuick.Controls::Menu::takeItem(int index) + + Removes and returns the item at \a index. \note The ownership of the item is transferred to the caller. */ -void QQuickMenu::removeItem(int index) +QQuickItem *QQuickMenu::takeItem(int index) { Q_D(QQuickMenu); const int count = d->contentModel->count(); if (index < 0 || index >= count) - return; + return nullptr; QQuickItem *item = itemAt(index); if (item) d->removeItem(index, item); + return item; } /*! |