Search code examples
javascriptqtqt4qmlqtquick2

What is the purpose of the default keyword in QML?


I'm trying to figure out what the default keyword in QML is.

In this example :http://qt-project.org/doc/qt-4.8/declarative-ui-components-tabwidget.html, how is it being used?

Since in the example it says that

any child items of the TabWidget are actually added to the 'stack' item's children.

But in main.qml, Rectangles are added as children of TabWidget.

Can someone give me a clearly understanding of the same?

Thanks in advance.


Solution

  • It is basically the default property of the QML object. That is, when you do not specify it explicitly, it will fall back to that. See the more detailed explanation and example in the the 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
    }
    

    The someText value could be assigned to in a MyLabel object definition, like this:

    MyLabel {
        Text { text: "world!" }
    }
    

    This has exactly the same effect as the following:

    MyLabel {
        someText: Text { text: "world!" }
    }
    

    Note that this should be more or less the same in Qt 4, too, as the concept.