Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add "property alias" only for testing at "QML" file.



  • I want to add "property alias" only for testing at QML file.
    Is there any way to do it ?

    //sample.qml
    
    Item {
    
        // I want to these properties for testing only.
        property alias innerListView: idListView
        property alias currentItem: idListView.currentItem
    
        ListView {
            id: idListView
            anchors.fill: parent
            clip: true
            model: weekModel
            delegate: Component
            {
                id: weekDelegate
                Item {
                    id: delegateItem
                    property alias text: menuText.text
                    
                    Text {
                        id :menuText
                        text: modelData
                    }
                }
            }
        }
        ListModel {
            id: weekModel
            ListElement{day: "Sunday"}
            ListElement{day: "Monday"}
            ListElement{day: "Tuesday"}
            ListElement{day: "Wednesday"}
            ListElement{day: "Thursday"}
            ListElement{day: "Saturday"}
        }
    }
    

    Is there any way to do like this ?

    //Rough idea
    
    #ifdef TEST
         property alias innerListView: idListView
    #endif
    

  • Moderators

    hi @dmhk_4ss and welcome

    something like this could be done.

    Inside your main set a property depending on specific circumstances, in this example, debug or not debug

        QQmlApplicationEngine engine;
    #ifdef QT_DEBUG
        engine.rootContext()->setContextProperty("debug", true);
    #else
        engine.rootContext()->setContextProperty("debug", false);
    #endif
    
    property var innerListView:  debug ? idListView : undefined //can't assign undefined to an alias -> var
    


  • Hi @J-Hilk san

    It is a great idea !
    Thank you very much.


Log in to reply