QML alias not working



  • Anyone any ideas why this code is not working? I have a loader in main which is happily loading files the c++ spits out to it but a slightly different mechanics is required for the username/password and I thought an alias would be simpler than a signal/slot. The loader (Main.qml) has an alias which I am trying to use to go from the username to the password (Loggon_username.qml)

    Main.qml

    @
    import QtQuick 2.0

    Rectangle
    {
    id: window
    property alias mainLoader: loader

    Component.onCompleted:
    {
        loader.forceActiveFocus()
    }
    
    Rectangle
    {
        id: promptsContainer
        width: parent.width
        height: prompts.height * 1.25
        color: "#2A51A3"
        anchors.top: parent.top
    
        Text
        {
            id: prompts //text showing what voice is saying
            anchors.centerIn: parent
            color: "orange"
            font.pointSize: 20 //coreMenu.promptsFontPointSize
            width: parent.width * 0.9
            wrapMode: Text.WordWrap
            clip: true
        }
    }
    
    Loader
    {
        id: loader
        height: window.height - promptsContainer.height
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        visible: source != ""
        source: "Loggon_password.qml"
        onSourceChanged: console.log(loader.source);
     }
    
     Keys.onPressed:
     {
        QMLManager.handleKey(event.key)
        loader.source =  QMLManager.getLoaderSource()
        console.log(loader.source)
     }
    

    }
    @

    Loggon_username.qml
    @import QtQuick 2.0

    Rectangle
    {
    anchors.fill: parent;

    Component.onCompleted:
    {
    
      // promptsBar.text = qsTr("Please enter your MTM username");
    }
    TextEntry
    {
        title: QMLManager.getTitle();
        onTextEntered:
        {
            QMLManager.setUsername(text);
            Main.mainLoader.setSource("Loggon_password.qml")
    
           // Main.mainLoader.source = "Help.qml"
        }
    }
    

    }
    @


  • Moderators

    "Main" is outside of scope in your Loggon_username. It won't work.



  • What do you mean by out of scope? The doc says this should work if the qml files are in same directory :-)


  • Moderators

    "Main" would need to be defined somewhere in Loggon_username.qml for it to be visible inside. Or maybe I misunderstood the situation.



    • if I import "Main.qml" the file will not load at all (which means it thinks there is a syntax error I think)
    • if main.qml file does not start with a capital when you type main.mainLoader it does not recognise it as a property
    • if Main.qml starts with 'M', when you are in the username file typing "Main." the text changes colour and you get a list of properties including the mainLoader

    ????

    I have tried this in a small test app and I get the same results


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.