qt - QML ToolButton 不调整大小

标签 qt qml qtquick2

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

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
                }


            }

显示我的 ToolButton 太小了:

enter image description here

我可以更改图像的高度并使其变大,但随后它会超出 ToolButton。当我尝试调整 ToolButton 的高度时,没有任何反应

调查结果

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

最佳答案

RowLayout 使用其子项的 implicitHeight 和 implicitWidth,并忽略高度和宽度。对于一些简单的项目(例如矩形),设置它们的高度也会改变它们的隐式高度,但对于像工具按钮这样的快速控件,情况并非如此。

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,您有两个选择:

关于qt - QML ToolButton 不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143935/

相关文章:

c++ - 如何在 Qt 中包含外部库

events - PyQt graphicsview 调整大小事件

c++ - QT官方文档中为什么使用static_cast

javascript - 在 QML 中创建一个新的 javascript 对象

Qt QML 下拉列表就像在 HTML 中一样

qml - 如何在不使用 MouseArea 的情况下更改光标的形状?

qt - 我可以使用 Qt 创建网页吗?

javascript - 如何从 QML 连接 C++ 对象的销毁信号?

c++ - QtQuick 2.1 从 TextInput 获取文本

javascript - 在 for-in 循环中对特定数组属性使用 JS Array.Reduce