首页 > 解决方案 > QML 上下文菜单大小根据其内容调整

问题描述

我对 Qt Quick 大小策略、宽度、隐式宽度、边界等感到困惑。Menu单击圆形按钮时,我有哪个弹出窗口。问题是菜单太大了。看随附的屏幕截图。项目应将对齐设置为右:

在此处输入图像描述

我可以通过设置Layout.fillWidth: trueicon来做到这一点Label

在此处输入图像描述

但是,菜单太大了,我有固定的大小300,但我想根据内容调整它的大小。此外,即使将间距、填充和边距设置为 0,项目之间的空间也会很大。我使用的是材质深色样式。这是代码:

Menu {
    id: roundButtonMenu
    modal: true
    spacing: 0
    padding: 0
    margins: 0

    Action { text: qsTr("File"); icon.name: FAIcons.faFile}
    Action { text: qsTr("Folder"); icon.name: FAIcons.faFolderOpen}
    Action { text: qsTr("Link"); icon.name: FAIcons.faLink}

    delegate: MenuItem {
        id: menuDelegate
        spacing: 0
        padding: 0
        contentItem: RowLayout {
            spacing: 0
            Label {
                id: mIcon
                text: menuDelegate.icon.name
                Layout.fillWidth: true
                Layout.alignment: Qt.AlignRight
                horizontalAlignment: Text.AlignRight
                font.family: FALoader.icons
                background: Rectangle {
                    color: "transparent"
                    border.color: "red"
                }
            }
            Label {
                id: mLabel
                Layout.fillWidth: false
                text: menuDelegate.text
                Layout.alignment: Qt.AlignRight
                background: Rectangle {
                    color: "transparent"
                    border.color: "red"
                }

            }
        }
        background: Rectangle {
            color: "transparent"

        }
    }

    background: Rectangle {
        implicitWidth: 300
        color: "transparent"
        border.color: "#21be2b"
        radius: 2
    }
}

CPRoundButton {
    id: btnAdd
    iconfa: FAIcons.faPlus
    anchors.right: parent.right
    anchors.bottom: parent.bottom
    onClicked: {
        roundButtonMenu.x = 10
        roundButtonMenu.y = 10
        roundButtonMenu.open()
    }
}

标签: qtqmlqtquickcontrols2

解决方案


推荐阅读