diff --git a/src/declarativeimports/plasmacomponents/qmenu.h b/src/declarativeimports/plasmacomponents/qmenu.h --- a/src/declarativeimports/plasmacomponents/qmenu.h +++ b/src/declarativeimports/plasmacomponents/qmenu.h @@ -93,6 +93,13 @@ */ 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 @@ 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 @@ void transientParentChanged(); void placementChanged(); void minimumWidthChanged(); + void maximumWidthChanged(); void triggered(QMenuItem *item); void triggeredIndex(int index); diff --git a/src/declarativeimports/plasmacomponents/qmenu.cpp b/src/declarativeimports/plasmacomponents/qmenu.cpp --- a/src/declarativeimports/plasmacomponents/qmenu.cpp +++ b/src/declarativeimports/plasmacomponents/qmenu.cpp @@ -153,6 +153,25 @@ } } +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/tests/components/menu.qml b/tests/components/menu.qml --- a/tests/components/menu.qml +++ b/tests/components/menu.qml @@ -68,5 +68,36 @@ 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 + } + } } }