[SOLVED]Modifying qml element properties from another qml file



  • Here is the problem I'm trying to solve:

    I would like to know how I can access an element inside a certain qml file from other qml files to change its properties, i.e. I have a TextInput element, and I want to clear its content on onButtonClick events located in other qml files.

    Thanks in advance for your help


  • Moderators

    @
    onButtonClick: {
    myOtherFileId.text = "";
    }
    @

    Is that what you want? It would work is this document is declared as an object with an ID in your "main" file. If it is declared elsewhere, you can still achieve communication, but you need to, for example, pass signals to file where TextInput is declared, and clear it from there in a slot similar to one I've written above.



  • Thanks for the quick reply. Unfortunately I don,t have it as a declared object in the main.qml so I think I should go for sending signals. I'll try to find out how, and will come back with news on how it went :)



  • Hi,

    I am not sure if this is want you want but if I understand you rigth this should help you:

    SomeComponent.qml
    @
    Item {

    property alias inputText: txtInput.text
    

    // some QML Items
    // ............

    TextInput {
    id: txtInput
    }
    }
    @

    Then in some other qml file you have:

    Other.qml
    @
    Item {

     Button {
          onbuttonClicked: smCmp.inputText = ""
     }
    
     SomeComponent {
         id: smCmp
     }
    

    }
    @



  • Well, I just made the signal solution work.
    I just added a signal to the qml file (e.g. called Menu)containing the TextInput element, like this:
    @
    signal clearText()
    onClearText: textInput.text =" "
    @

    Now, if for example I have a declared object of the Menu qml file with id:myMenu, I can emit the clearText() signal to get the TextInput cleared from wherever I want, like this:

    @myMenu.clearText()@

    I'm not sure, but I guess the second solution (by task_struct) won't work in my case, because the object is declared somewhere else than where I want to raise the clearText() event.

    Thanks for your suggestions anyhow :)


Log in to reply
 

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