[PlasmaComponents Menu] Don't crash on null action

You can assign a QAction as "action", this way you can just pass it e.g. plasmoid.action("configure").
However, when assigning a null action as can happen with kiosk restrictions, it would crash
as it assigns m_action the nullptr but never checks for that.

This patch ensures we always have a QAction, creating a new empty one, if neccessary.
Also deletes our own action if an external one is assigned

Differential Revision: https://phabricator.kde.org/D6608
This commit is contained in:
Kai Uwe Broulik 2017-07-11 12:23:39 +02:00
parent c00069c43d
commit 21f954d94d
2 changed files with 32 additions and 2 deletions

View File

@ -38,8 +38,24 @@ void QMenuItem::setAction(QAction *a)
if (m_action != a) { if (m_action != a) {
if (m_action) { if (m_action) {
disconnect(m_action, 0, this, 0); disconnect(m_action, 0, this, 0);
if (m_action->parent() == this) {
delete m_action;
m_action = nullptr;
}
} }
m_action = a;
if (a) {
m_action = a;
} else {
// don't end up with no action, create an invisible one instead
m_action = new QAction(this);
m_action->setVisible(false);
}
setVisible(m_action->isVisible());
setEnabled(m_action->isEnabled());
connect(m_action, &QAction::changed, this, &QMenuItem::textChanged); connect(m_action, &QAction::changed, this, &QMenuItem::textChanged);
connect(m_action, &QAction::changed, this, &QMenuItem::checkableChanged); connect(m_action, &QAction::changed, this, &QMenuItem::checkableChanged);
connect(m_action, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool))); connect(m_action, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));

View File

@ -3,7 +3,7 @@ import org.kde.plasma.components 2.0 as PlasmaComponents
Rectangle { Rectangle {
width: 600 width: 600
height: 200 height: 250
color: "white" color: "white"
Flow { Flow {
@ -99,5 +99,19 @@ Rectangle {
checked: true checked: true
} }
} }
PlasmaComponents.Button {
text: "Don't crash on null MenuItem action"
onClicked: noActionCrashMenu.open(0, height)
PlasmaComponents.Menu {
id: noActionCrashMenu
PlasmaComponents.MenuItem { text: "This is an item" }
PlasmaComponents.MenuItem { text: "Below me should NOT be an empty item"}
PlasmaComponents.MenuItem { action: null }
PlasmaComponents.MenuItem { text: "I am not empty" }
}
}
} }
} }