我想知道根据 QML 代码中的条件创建矩形、文本或按钮的最佳方法是什么。
这是一个伪代码:
import QtQuick 2.0
Item{
property string name = "rect" or "text" or "button"
id:root
if (name === "rect")
Rectangle {
//properties
parent: root
}
else if (name === "text")
Text {
//properties
parent: root
}
else if (name === "button")
Button {
//properties
parent: root
}
}
最佳答案
用Loader
试试吧
Loader {
property bool shouldBeText
Component { id: rect; Rectangle {}}
Component { id: text; Text {}}
sourceComponent: shouldBeText ? text : rect
}
关于qt - QML如何根据条件创建不同的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070734/