不使用布局的 QML 基线对齐

问题描述 投票:0回答:1

我想使用基线对齐锚点,而不使用任何花哨的 RowLayouts 等。我想使用标签的底部锚点与文本编辑的基线锚定。我认为,没有文本的控件的基线是顶部锚点,而对于带有文本的控件来说,基线是顶部锚点和底部锚点之间的某个位置。但对于我的 TextEdit 来说,它是顶部锚点,这是意想不到的

        Rectangle {
            width: 320
            height: 100
            border.width:  1

            Label {
                id: l
                text: "Description "
                height: 40
                anchors.bottom: te1.baseline
            }
            TextField {
                id: te1
                text: "Some test"
                height: 35
                anchors.left: l.right
                y: parent.height /  2
            }
        }

我尝试使用 Qt5&6 以及默认控件和 QtQuick.Controls 进行演示。由于某种原因,文本编辑的基线是顶部。为什么?

更新:我将所需的结果放在实际结果下方的屏幕截图上。我尝试使用控件的大小和字体的度量来手动计算标签的偏移量。可悲的是,我仍然有魔法常量,它不能在不同机器之间移植

FontMetrics {
    id: fontMetrics
    font.family: "Arial"
    font.pointSize: defaultFontSize
}

Rectangle {
    width: 320
    height: 120
    border.width:  1
    Text {
        id: l3
        text: "Description:"
        height: 15
        width: 150
        x: 0
        y: te3.y + te3.height / 2 - fontMetrics.height / 2 - 4
        font.family: "Arial";
        font.pointSize: defaultFontSize
    }
    TextField {
        id: te3
        text: "asdf2222"
        font.family: "Arial"
        font.pointSize: defaultFontSize
        background: Rectangle { color: "lightgray" }
        height: 50
        width: 100
        y: parent.height /  2
        anchors.left: l3.right
    }
}
qml alignment qtquick2 qtquickcontrols2 baseline
1个回答
0
投票

下面的内容可以帮您完成工作吗?

Rectangle {
            width: 320
            height: 200
            border.width:  1
            color: "black"
            Label {
                id: l
                height: 40
                text: "Description: "
                anchors.baseline: te1.baseline
                //y: te1.y
            }
            TextField {
                id: te1
                height:35
                text: "Some test"
                anchors.left: l.right
                y: parent.height /  2
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.