Qt QML矩形:在由布局管理的项上检测到锚定。这是未定义的行为;改用Layout.alignment

Qt QML矩形:在由布局管理的项上检测到锚定。这是未定义的行为;改用Layout.alignment,qt,qml,Qt,Qml,我正在使用qml在qt中定制控件 尝试使用qt expect 5.11的早期版本,它正在工作,我不知道我需要更改什么,请任何人帮助 Rectangle{ width: parent.width - 30 height: 25 anchors.leftMargin: 15 anchors.left: parent.left color: "transparent" visible: (!auto_start) RowLayout{

我正在使用qml在qt中定制控件

尝试使用qt expect 5.11的早期版本,它正在工作,我不知道我需要更改什么,请任何人帮助


Rectangle{
    width: parent.width - 30
    height: 25
    anchors.leftMargin: 15
    anchors.left: parent.left
    color: "transparent"
    visible: (!auto_start)
    RowLayout{
        anchors.fill: parent
        Text{
            text: "Frame Rate:"
            anchors.leftMargin: 10
            anchors.left: parent.left
            font.pointSize: 13
            font.family: fontFamily.name
        }
        Text{
            id: framesValueLabel
            text: "0 fps"
            font.bold: true
            anchors.right: parent.right
            anchors.rightMargin: 10
            font.pointSize: 13
            font.family: fontFamily.name
        }
    }
}

在以前的版本中,它工作正常,现在qt5.11工作正常,但在控制台中显示许多警告错误

您不应该将锚与布局组合在一起,因为它们都完成定位项目的类似任务

如警告所示:

Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.
也许在以前的版本中,Qt并没有那么聪明地检测到这些可能的错误,所以我没有指出

因此,解决方案是使用Layout.alignment、Layout.leftMargin和Layout.righmargin:

Rectangle{
    width: parent.width - 30
    height: 25
    anchors.leftMargin: 15
    anchors.left: parent.left
    color: "blue"
    visible: (!auto_start)
    RowLayout{
        anchors.fill: parent
        Text{
            text: "Frame Rate:"
            Layout.leftMargin: 10
            Layout.alignment : Qt.AlignLeft
            font.pointSize: 13
            font.family: fontFamily.name
        }
        Text{
            id: framesValueLabel
            text: "0 fps"
            font.bold: true
            Layout.alignment : Qt.AlignRight
            Layout.rightMargin: 10
            font.pointSize: 13
            font.family: fontFamily.name
        }
    }
}

更好地解释自己,你的目标是什么?显示错误消息等。警告消息:qrc:qml/main.qml:382:4:qml矩形:在布局管理的项目上检测到锚。这是未定义的行为;改用Layout.alignment。