我想弄清楚 QML 中的默认关键字是什么。
在这个例子中:http://qt-project.org/doc/qt-4.8/declarative-ui-components-tabwidget.html , 它是如何使用的?
因为在例子中它说
any child items of the TabWidget are actually added to the 'stack' item's children.
但在 main.qml 中,矩形被添加为 TabWidget 的子项。
谁能给我一个清晰的理解?
提前致谢。
最佳答案
它基本上是 QML 对象的默认属性。也就是说,当你没有明确指定它时,它会退回到那个。请参阅 documentation 中更详细的解释和示例:
Default Properties An object definition can have a single default property. A default property is the property to which a value is assigned if an object is declared within another object's definition without declaring it as a value for a particular property. Declaring a property with the optional default keyword marks it as the default property. For example, say there is a file MyLabel.qml with a default property someText:
// MyLabel.qml
import QtQuick 2.0
Text {
default property var someText
text: "Hello, " + someText.text
}
可以在 MyLabel 对象定义中分配 someText 值,如下所示:
MyLabel {
Text { text: "world!" }
}
这与下面的效果完全相同:
MyLabel {
someText: Text { text: "world!" }
}
请注意,这在 Qt 4 中也应该或多或少与概念相同。
关于javascript - QML 中 default 关键字的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027118/