我有以下QML代码:
ApplicationWindow
{
id: ueMainWindow
title: qsTr("testApp")
width: Screen.desktopAvailableWidth
height: Screen.desktopAvailableWidth
visibility: "FullScreen"
visible: true
opacity: 1.0
contentOrientation: Qt.LandscapeOrientation
color: "black"
UeCentralWidget
{
id: ueCentralWidget
implicitWidth: contentItem.implicitWidth
implicitHeight: contentItem.implicitHeight
//anchors.centerIn: parent
visible: false
enabled: false
opacity: 0.0
} // Rectangle
} // ApplicationWindow
现在,如果我取消注释行anchors.centerIn: parent
,窗口位置是正确的,我得到以下QML警告/错误:
文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitHeight”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitHeight”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitHeight”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitHeight”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 qrc:/main.qml:34:5:QML项目:检测到可能的锚点循环 centerIn。 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitWidth”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:检测到属性“implicitHeight”的绑定循环 文件:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ApplicationWindow.qml:236:9: QML ContentItem:为属性“implicitHeight”检测到绑定循环
如何在不使用Item
的情况下将此anchors.centerIn: parent
居中,以便我可以摆脱这些错误/警告?
答案 0 :(得分:2)
仔细查看ContentItem
代码
function __calcImplicitWidth() {
if (__layoutItem && __layoutItem.anchors.fill)
return __calcImplicit('Width')
return contentItem.childrenRect.x + contentItem.childrenRect.width
}
function __calcImplicitHeight() {
if (__layoutItem && __layoutItem.anchors.fill)
return __calcImplicit('Height')
return contentItem.childrenRect.y + contentItem.childrenRect.height
}
内容项的 implicitheight
和implicitWidth
取决于其子项的x, y, width and height
,
您的对象(UeCentralWidget
)是contentItem的子对象。
在你写作的时候
implicitWidth: contentItem.implicitWidth
implicitHeight: contentItem.implicitHeight
你正在进行绑定循环。
anchors.centerIn: parent
不是问题,请尝试将代码更改为:
implicitWidth: 20
implicitHeight: 20
我认为,正确的解决方案是为对象提供静态宽度和高度。
如果不接受此警告,请使用解决方法导致问题无法解决问题。
答案 1 :(得分:1)
也许有不同的方法来解决这个问题,但我知道的最简单的方法是在implicitWidth
完全加载后设置implicitHeight
和UeCentralWidget
。
请让我更改UeCentralWidget
并改为使用Rectangle
。下一个代码工作正常,因为我们在建立完整的QML环境后设置属性。
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow
{
id: ueMainWindow
title: qsTr("testApp")
width: Screen.desktopAvailableWidth
height: Screen.desktopAvailableWidth
visibility: "FullScreen"
visible: true
opacity: 1.0
contentOrientation: Qt.LandscapeOrientation
color: "black"
Rectangle {
id: rectangle
anchors.centerIn: parent
color: "red"
visible: true
enabled: false
// implicitWidth: contentItem.implicitWidth
// implicitHeight: contentItem.implicitHeight
Component.onCompleted: {
rectangle.implicitWidth = contentItem.implicitWidth
rectangle.implicitHeight = contentItem.implicitHeight
}
} // Rectangle
} // ApplicationWindow