Qml RowLayout忽略Layout.minimumWidth

Qml RowLayout忽略Layout.minimumWidth,qml,qt5,qtquick2,Qml,Qt5,Qtquick2,当窗口太小时,应适当删除文本和text2(当空间不足以覆盖整个字符串时,显示三个点)。Layout.preferredWidth设置为text.implicitWidth,并添加了一些填充,但我甚至设置了Layout.minimumWidth,因此当空间不足时可以省略文本。似乎行布局忽略了布局.minimumWidth并保留了布局.preferredWidth,因此文本仍然具有布局.preferredWidth的宽度,并且永远不会被忽略。怎么了 import QtQuick 2.5 import

当窗口太小时,应适当删除
文本
text2
(当空间不足以覆盖整个字符串时,显示三个点)。
Layout.preferredWidth
设置为
text.implicitWidth
,并添加了一些填充,但我甚至设置了
Layout.minimumWidth
,因此当空间不足时可以省略文本。似乎
行布局
忽略了
布局.minimumWidth
并保留了
布局.preferredWidth
,因此文本仍然具有
布局.preferredWidth
宽度
,并且永远不会被忽略。怎么了

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    contentItem.minimumWidth: 50

    RowLayout {
        id: ntoolbarline

        anchors.fill: parent

        Rectangle {
            color: "red"

            Layout.preferredWidth: text.implicitWidth + 40
            Layout.preferredHeight: 25
            Layout.minimumWidth: 25
            Layout.minimumHeight: 25
            Layout.maximumWidth: text.implicitWidth + 40

            Text {
                id: text
                anchors.centerIn: parent
                text: "Foooooooo"
                elide: Text.ElideRight
                width: parent.width - 40
                renderType: Text.NativeRendering
            }
        }

        Rectangle {
            color: "red"

            Layout.preferredWidth: text2.implicitWidth + 40
            Layout.preferredHeight: 25
            Layout.minimumWidth: 25
            Layout.minimumHeight: 25
            Layout.maximumWidth: text2.implicitWidth + 40

            onWidthChanged: console.log("layout item width: " + width) // only one output when app started

            Text {
                id: text2
                anchors.centerIn: parent
                text: "Baaaaaaaar"
                elide: Text.ElideRight
                width: parent.width - 40
                renderType: Text.NativeRendering
            }
        }

        Item {
            id: spacer
            Layout.fillWidth: true
            onWidthChanged: console.log("spacer width: " + width) // outputs whenever the window is resized
        }

    }

}

这里有两个主要问题:

  • 未定义填充宽度
  • 文本
    中心
    矩形
  • 没有1。
    矩形
    s不能在所需约束内增长或收缩。利用2。由于
    Text
    元素的大小和边框没有特定限制,因此
    Text
    无法正确检查是否需要elide。正确的方法是使用
    填充
    并通过
    文本
    垂直对齐
    水平对齐
    设置对齐

    最终重新访问的代码如下所示:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.1
    
    ApplicationWindow {
        id: window
        visible: true
        width: 640
        height: 480
        minimumWidth: 50
        title: qsTr("Hello World")
    
        RowLayout {
            id: ntoolbarline
            anchors.fill: parent
    
            Rectangle {
                color: "red"
                Layout.fillWidth: true
                Layout.preferredWidth: text.implicitWidth + 40
                Layout.preferredHeight: 25
                Layout.minimumWidth: 25
                Layout.maximumWidth: text.implicitWidth + 40
    
                Text {
                    id: text
                    anchors.fill: parent
                    text: "Foooooooo"
                    elide: Text.ElideRight
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    renderType: Text.NativeRendering
                }
            }
    
            Rectangle {
                color: "red"
                Layout.fillWidth: true
                Layout.preferredWidth: text.implicitWidth + 40
                Layout.preferredHeight: 25
                Layout.minimumWidth: 25
                Layout.maximumWidth: text.implicitWidth + 40
    
                Text {
                    id: text2
                    anchors.fill: parent
                    text: "Baaaaaaaar"
                    elide: Text.ElideRight
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    renderType: Text.NativeRendering
                }
            }
    
            Item {
                id: spacer
                Layout.fillWidth: true
            }
        }
    }