Qt 绑定到QML小部件';s渲染高度

Qt 绑定到QML小部件';s渲染高度,qt,layout,qml,Qt,Layout,Qml,假设我有一个文本字段和一个按钮。我想将按钮的宽度和高度设置为文本字段的渲染高度,但它不起作用 该按钮似乎忽略了与文本字段高度的绑定。我的理论是,由于没有显式设置此属性,QML不知道要为按钮指定哪个宽度/高度 采用文本字段的实际渲染高度的正确方法是什么?如果使用布局,则不应使用宽度或高度,如果要获得相同的高度,则必须使用implicitWidth或implicitHeight,如果希望项目占据行的高度,则必须使用Layout.fillHeight:true。宽度也是这样 import QtQui

假设我有一个文本字段和一个按钮。我想将按钮的宽度和高度设置为文本字段的渲染高度,但它不起作用

该按钮似乎忽略了与文本字段高度的绑定。我的理论是,由于没有显式设置此属性,QML不知道要为按钮指定哪个宽度/高度


采用文本字段的实际渲染高度的正确方法是什么?

如果使用布局,则不应使用宽度或高度,如果要获得相同的高度,则必须使用
implicitWidth
implicitHeight
,如果希望项目占据行的高度,则必须使用
Layout.fillHeight:true
。宽度也是这样

import QtQuick 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Window 2.10

Window {
    id: window
    visible: true
    width: 640
    height: 200
    color: "#f0eded"
    title: qsTr("Hello World")

    RowLayout {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        TextField {
            id: txtPassword
            text: qsTr("Text Field")
            font.pointSize: 22
        }

        Button {
            id: btnSubmit
            implicitHeight: txtPassword.implicitHeight // or Layout.fillHeight: true
            implicitWidth: implicitHeight
            text: qsTr("»")
        }
    }
}

或者,您可以使用:


如果使用布局,则不应使用宽度或高度,如果要获得相同的高度,则必须使用
implicitWidth
implicitHeight
,如果希望项目占据行的高度,则必须使用
layout.fillHeight:true
。宽度也是这样

import QtQuick 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Window 2.10

Window {
    id: window
    visible: true
    width: 640
    height: 200
    color: "#f0eded"
    title: qsTr("Hello World")

    RowLayout {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        TextField {
            id: txtPassword
            text: qsTr("Text Field")
            font.pointSize: 22
        }

        Button {
            id: btnSubmit
            implicitHeight: txtPassword.implicitHeight // or Layout.fillHeight: true
            implicitWidth: implicitHeight
            text: qsTr("»")
        }
    }
}

或者,您可以使用:

import QtQuick 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Window 2.10

Window {
    id: window
    visible: true
    width: 640
    height: 200
    color: "#f0eded"
    title: qsTr("Hello World")

    Row {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        spacing: 5

        TextField {
            id: txtPassword
            text: qsTr("Text Field")
            font.pointSize: 22
        }

        Button {
            id: btnSubmit
            width: txtPassword.height
            height: txtPassword.height
            text: qsTr("»")
        }
    }
}