不使用anchors.centerIn:parent

时间:2015-10-16 17:40:52

标签: qml

我有以下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居中,以便我可以摆脱这些错误/警告?

2 个答案:

答案 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
    } 
内容项的

implicitheightimplicitWidth取决于其子项的x, y, width and height, 您的对象(UeCentralWidget)是contentItem的子对象。 在你写作的时候

   implicitWidth: contentItem.implicitWidth
   implicitHeight: contentItem.implicitHeight

你正在进行绑定循环。

anchors.centerIn: parent不是问题,请尝试将代码更改为:

implicitWidth:  20
implicitHeight: 20

我认为,正确的解决方案是为对象提供静态宽度和高度。

如果不接受此警告,请使用解决方法导致问题无法解决问题。

答案 1 :(得分:1)

也许有不同的方法来解决这个问题,但我知道的最简单的方法是在implicitWidth完全加载后设置implicitHeightUeCentralWidget

请让我更改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