Important: Please read the Qt Code of Conduct -

Column Width Matching with Children

  • Hi. I want to create something like a contextmenu that is a Column and I want the delegate to be a Label and I want the column's width be the delegate's width that has the maximum width among the others. how can I do that?

  • Lifetime Qt Champion


    Doesn't QMenu already provide what you need for a contextual menu ?

  • Hi @SGaist
    I know that it does but I want to create my own and more importantly I want to know how to do that because I will need it elsewhere

  • Lifetime Qt Champion

    Why do you want to create your own ?

    You take a look a QMenu's sources.

  • Hi @SGaist
    I needed it to create something like a dialog
    I looked at the QMenu's source but it didn't help. but then I figured it out after some thinking:

    import QtQuick 2.7
    import QtQuick.Controls 2.0

    Rectangle {
    id: root
    clip: true

    color: "cyan"
    width: listView.width
    height: listView.height
    property color textColor: "black"
    property var items: [
        "Open", "Save", "Exit"
        id: testLabel
        visible: false
        text: "test"
        id: listView
        width: 0
        height: (testLabel.height*3/2)*count
        anchors.centerIn: parent
        model: items
        delegate: BaseButton{
            id: base
            mainColor: root.color
            width: label.width+label.height/2
            height: label.height*3/2
                id: label
                anchors.centerIn: parent
                text: items[index]
                color: root.textColor
            Component.onCompleted: listView.width = base.width>listView.width ? base.width:listView.width


  • Lifetime Qt Champion

    Sorry, my bad, I somehow missed that you posted that on the QtQuick sub-forum.

Log in to reply