我想根据条件加载主窗口:
Loader {
source: blocky ? "BlockyMainWindow.qml" : "RoundyMainWindow.qml"
}
但要绑定的属性是相同的:width
/ height
,anchors
,transform
。
仅当所有内容都写在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 }
如何简化?
答案 0 :(得分:3)
您可以将这些属性绑定移动到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。