【问题标题】:Binding properties of the component created by the LoaderLoader创建的组件的绑定属性
【发布时间】:2015-08-26 07:20:18
【问题描述】:

我想根据条件加载主窗口:

Loader {
    source: blocky ? "BlockyMainWindow.qml" : "RoundyMainWindow.qml"
}

但是要绑定的属性是一样的:width/heightanchorstransform

仅当所有内容都写入onLoaded 处理程序时才有效。此外,您必须编写两次:一次执行Qt.binding,第二次仅分配值,因为如果不更改某些值,绑定将无法启动。

属性:

    width: ContentOrientation.rotated ? parent.height : parent.width
    height: ContentOrientation.rotated ? parent.width : parent.height

    anchors.left: parent.left
    anchors.top: ContentOrientation.rotated ? parent.bottom : parent.top
    transform: Rotation { origin.x: 0; origin.y: 0; angle: ContentOrientation.rotated ? -90 : 0 }

如何让它变得简单?

【问题讨论】:

  • 你能显示这些你正在绑定的相同属性吗?

标签: qt qml qtquick2


【解决方案1】:

您可能可以将这些属性绑定移动到 Loader 本身:

Loader {
    source: blocky ? "BlockyMainWindow.qml" : "RoundyMainWindow.qml"

    width: ContentOrientation.rotated ? parent.height : parent.width
    height: ContentOrientation.rotated ? parent.width : parent.height
    // etc.
}

例如:

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: window
    visible: true

    Component {
        id: blockyComponent

        Rectangle {
            color: "red"

            Text {
                text: "Blocky"
            }
        }
    }

    Component {
        id: roundedComponent

        Rectangle {
            color: "green"
            radius: 20

            Text {
                text: "Rounded"
            }
        }
    }

    Loader {
        sourceComponent: loaderType.checked ? blockyComponent : roundedComponent
        anchors.fill: parent
        rotation: 90
    }

    Switch {
        id: loaderType
    }
}

更多信息请参见Loader sizing behavior

【讨论】:

  • 如果我想绑定自定义属性怎么办?
猜你喜欢
  • 2014-05-23
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2015-07-11
相关资源
最近更新 更多