diff --git a/src/declarativeimports/plasmacomponents/qmenuitem.cpp b/src/declarativeimports/plasmacomponents/qmenuitem.cpp --- a/src/declarativeimports/plasmacomponents/qmenuitem.cpp +++ b/src/declarativeimports/plasmacomponents/qmenuitem.cpp @@ -38,8 +38,24 @@ if (m_action != a) { if (m_action) { disconnect(m_action, 0, this, 0); + + if (m_action->parent() == this) { + delete m_action; + m_action = nullptr; + } + } + + 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); } - m_action = a; + + setVisible(m_action->isVisible()); + setEnabled(m_action->isEnabled()); + connect(m_action, &QAction::changed, this, &QMenuItem::textChanged); connect(m_action, &QAction::changed, this, &QMenuItem::checkableChanged); connect(m_action, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool))); diff --git a/tests/components/menu.qml b/tests/components/menu.qml --- a/tests/components/menu.qml +++ b/tests/components/menu.qml @@ -3,7 +3,7 @@ Rectangle { width: 600 - height: 200 + height: 250 color: "white" Flow { @@ -99,5 +99,19 @@ 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" } + } + } } }