QML上下文菜单大小已调整为其内容

时间:2019-07-13 19:40:17

标签: qt qml qtquickcontrols2

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

enter image description here

我可以通过在图标Layout.fillWidth: true上设置Label来做到这一点:

enter image description here

但是菜单仍然很大,我的大小为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()
    }
}

0 个答案:

没有答案