[Development] [QML] Using a list as a default property

Mitch Curtis mitch.curtis at digia.com
Fri Oct 11 14:05:18 CEST 2013


Hi.

I'm curious as to why the following workaround is necessary and how it 
works. It seems that I can't declare a default list property, but I can 
if I alias it. See: https://bugreports.qt-project.org/browse/QTBUG-10822

qml-test.qml:

import QtQuick 2.1
import QtQuick.Controls 1.0

Rectangle {
     width: 480
     height: 800

     Test {
         id: test
         MenuItem {
             text: "toot"
         }
         MenuItem {
             text: "boot"
         }
         MenuItem {
             text: "loot"
         }
     }

     Component.onCompleted: {
         for (var i = 0; i < test.menuItems.length; ++i) {
             print(test.menuItems[i].text)
         }
     }
}

Test.qml:

import QtQuick 2.1
import QtQuick.Controls 1.1

Item {
     default property alias menuItems: defaultPropertyHack.menuItems

     QtObject {
         // Can't specify a default property as a "template" list
         // https://bugreports.qt-project.org/browse/QTBUG-10822
         id: defaultPropertyHack
         property list<MenuItem> menuItems
     }

     // qml-test.qml:9 Type Test unavailable
     // qml-test/Test.qml:14 Unexpected token `<'
     // qml-test/Test.qml:14 Expected token `:'
//    default property list<MenuItem> menuItems
}

Cheers.



More information about the Development mailing list