代码之家  ›  专栏  ›  技术社区  ›  adviner

QML工具按钮未调整大小

  •  3
  • adviner  · 技术社区  · 8 年前

    我不明白为什么我的工具按钮这么小。工具栏是顶部的橙色矩形。以下代码:

    Item{
    
        ToolBar{
            id: toolbar
            width: parent.width
            height: scaleDP(0.29)
            anchors.top: parent.top
    
            background: Rectangle{
                color: "orange"
            }
    
            RowLayout{
                anchors.fill: parent
    
    
                ToolButton {
                    id: buttonBack
                    height: parent.height
                    width: parent.height
                    Image {
                        id: imageBack
                        source: "qrc:/assets/images/left-filled-black-50.png"
                        anchors.fill: parent
                        anchors.margins: 4
                    }
    
    
                }
    

    显示的工具按钮太小:

    enter image description here

    我可以更改图像的高度并使其变大,但它位于工具按钮之外。当我尝试调整ToolButton的高度时,什么都没有发生

    调查结果

    问题似乎出在行布局上。如果我将其更改为行或矩形,则图标会按预期调整大小。

    1 回复  |  直到 8 年前
        1
  •  4
  •   Yoann Quenach de Quivillic    8 年前

    RowLayout使用其子级的implicitHeight和implicitWidth,并忽略高度和宽度。对于一些简单的项目(如矩形),设置其高度也会更改其隐含高度,但对于ToolButton之类的快速控件,情况并非如此。

    RowLayout{
        height: 20
        width: parent.width
        // WON'T WORK
        ToolButton {
            id: buttonBack1
            height: parent.height
            width: parent.height
        }
        // OK
        ToolButton {
            id: buttonBack2
            Layout.preferredHeight: parent.height
            Layout.preferredWidth: parent.height
        }
        // OK TOO
        ToolButton {
            id: buttonBack3
            implicitHeight: parent.height
            implicitWidth: parent.height
        }
    }
    

    如果你还需要 RowLayout ,您有两个选项: