diff --git a/src/declarativeimports/plasmacomponents/qmenu.cpp b/src/declarativeimports/plasmacomponents/qmenu.cpp index 9cd3ca682..35e42df24 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.cpp +++ b/src/declarativeimports/plasmacomponents/qmenu.cpp @@ -153,6 +153,25 @@ void QMenuProxy::setMinimumWidth(int width) } } +int QMenuProxy::maximumWidth() const +{ + return m_menu->maximumWidth(); +} + +void QMenuProxy::setMaximumWidth(int width) +{ + if (m_menu->maximumWidth() != width) { + m_menu->setMaximumWidth(width); + + emit maximumWidthChanged(); + } +} + +void QMenuProxy::resetMaximumWidth() +{ + setMaximumWidth(QWIDGETSIZE_MAX); +} + bool QMenuProxy::event(QEvent *event) { switch (event->type()) { diff --git a/src/declarativeimports/plasmacomponents/qmenu.h b/src/declarativeimports/plasmacomponents/qmenu.h index 7b4360cc2..cde223e58 100644 --- a/src/declarativeimports/plasmacomponents/qmenu.h +++ b/src/declarativeimports/plasmacomponents/qmenu.h @@ -93,6 +93,13 @@ class QMenuProxy : public QObject */ Q_PROPERTY(int minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged) + /** + * A maximum width for the menu. + * + * @since 5.31 + */ + Q_PROPERTY(int maximumWidth READ maximumWidth WRITE setMaximumWidth RESET resetMaximumWidth NOTIFY maximumWidthChanged) + public: QMenuProxy(QObject *parent = 0); ~QMenuProxy(); @@ -114,6 +121,10 @@ public: int minimumWidth() const; void setMinimumWidth(int width); + int maximumWidth() const; + void setMaximumWidth(int maximumWidth); + void resetMaximumWidth(); + /** * This opens the menu at position x,y on the given visualParent. By default x and y are set to 0 */ @@ -163,6 +174,7 @@ Q_SIGNALS: void transientParentChanged(); void placementChanged(); void minimumWidthChanged(); + void maximumWidthChanged(); void triggered(QMenuItem *item); void triggeredIndex(int index); diff --git a/tests/components/menu.qml b/tests/components/menu.qml index 429cdb0cb..84f5a3b15 100644 --- a/tests/components/menu.qml +++ b/tests/components/menu.qml @@ -68,5 +68,36 @@ Rectangle { PlasmaComponents.MenuItem { text: "Another item" } } } + + Row { + spacing: units.smallSpacing + + PlasmaComponents.Button { + id: minMaxButton + text: "Fixed minimum and maximum width" + onClicked: minMaxMenu.open(0, height) + + PlasmaComponents.Menu { + id: minMaxMenu + + minimumWidth: minMaxButton.width + maximumWidth: limitMenuMaxWidth.checked ? minMaxButton.width : undefined // has a RESET property + + PlasmaComponents.MenuItem { text: "Hello" } + PlasmaComponents.MenuItem { text: "This is just a simple" } + PlasmaComponents.MenuItem { text: "Menu" } + PlasmaComponents.MenuItem { text: "with some very very long text in one item that will " + + "make the menu super huge if you don't do anything about it" } + PlasmaComponents.MenuItem { text: "and other stuff" } + } + } + + PlasmaComponents.CheckBox { + id: limitMenuMaxWidth + anchors.verticalCenter: parent.verticalCenter + text: "Limit maximum width" + checked: true + } + } } }