From ec8952cec9b44e7ab77c5450d8d6fb6579c21555 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 30 Jul 2018 14:28:44 +0200 Subject: Fix Menu not being dismissed when the triggered item disables itself Problem Consider the following code: Menu { title: "Menu" Action { text: "Item" onTriggered: enabled = false } } A MenuItem (AbstractButton) is created for the Action, and when it is clicked, this function is called: void QQuickAbstractButtonPrivate::trigger() { Q_Q(QQuickAbstractButton); if (action && action->isEnabled()) QQuickActionPrivate::get(action)->trigger(q, false); else if (effectiveEnable) emit q->clicked(); } QQuickActionPrivate::get(action)->trigger(q, false) results in this function being called: void QQuickAbstractButtonPrivate::click() { Q_Q(QQuickAbstractButton); if (effectiveEnable) emit q->clicked(); } Since the action (and hence the menu item) was disabled in the signal handler, the effectiveEnable check fails and clicked() is not emitted. This causes the menu to not be dismissed. Solution Before calling QQuickActionPrivate::get(action)->trigger(), store the button's enabled state. If triggering the action causes the action to be disabled (due to the signal handler), we can then choose whether or not we emit QQuickAbstractButton::clicked(). Specifically, we emit clicked() if: - we were enabled before triggering the action, and - we have no associated action, or it's no longer enabled Task-number: QTBUG-69682 Change-Id: Ib4e3c313b776decc74089a6beffe415605c430be Reviewed-by: Richard Moe Gustavsen --- src/quicktemplates2/qquickabstractbutton.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/quicktemplates2') diff --git a/src/quicktemplates2/qquickabstractbutton.cpp b/src/quicktemplates2/qquickabstractbutton.cpp index 9157b4f9..3b41f34c 100644 --- a/src/quicktemplates2/qquickabstractbutton.cpp +++ b/src/quicktemplates2/qquickabstractbutton.cpp @@ -324,9 +324,10 @@ void QQuickAbstractButtonPrivate::click() void QQuickAbstractButtonPrivate::trigger() { Q_Q(QQuickAbstractButton); + const bool wasEnabled = effectiveEnable; if (action && action->isEnabled()) QQuickActionPrivate::get(action)->trigger(q, false); - else if (effectiveEnable) + if (wasEnabled && (!action || !action->isEnabled())) emit q->clicked(); } -- cgit v1.2.3