javascript - QML 中 default 关键字的用途是什么?

标签 javascript qt qt4 qml qtquick2

我想弄清楚 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/

相关文章:

javascript - 如何获取 HTML 元素中的 href?

javascript - 为什么我的维吉尼亚密码算法的解密输出中会出现随机未定义的字符?

javascript - 检查数字的反转是否等于数字

c++ - 警告信息 “No matching signal for”

javascript - Angular Material 中具有相同内容的选项卡

c++ - 无法在 MFC 应用程序中显示模态 QProgressDialog

c++ - QT静态库未定义对静态变量的引用

c++ - Qt C++ 在按键时播放声音 - 几秒钟后停止工作

c++ - Qt 停止应用程序一段时间

python - PyQt4:在 trUtf8 字符串中检测到非 ASCII 字符