[SOLVED]Accessing variables of other QML files



  • Hello!

    I'm pretty new to QML and I haven't found any solution to the problem I'm facing at the moment.

    Thats how my two QML files look like:

    Button.qml
    @
    Button {

    // some properties
    
    onButtonClick: {
        variable.group = 1
        setScreen("MainNewsFeeds")
    }
    

    }
    @

    File.qml
    @
    Item {
    id: main
    objectName: "MainNewsFeeds"

    Item {
        id: variable
        property int group: 0
        }
    

    }
    @

    The problem is that the file Button.qml doesn't find variables that are located in File.qml

    I would really appreciate any help!


  • Moderators

    That is right - it should not see them in that configuration.

    In File.qml, handle the signal emitted by your button (buttonClick signal). Don't change variable.group from within the Button that is in a completely different and unrelated file! They don't "see" each other.

    Or, place the button as a kid of variable, and handle the signal there (again: in File.qml, not Button.qml. The latter file does not know anything about properties of File.qml).



  • [quote author="sierdzio" date="1345108278"]That is right - it should not see them in that configuration.

    In File.qml, handle the signal emitted by your button (buttonClick signal). Don't change variable.group from within the Button that is in a completely different and unrelated file! They don't "see" each other.

    Or, place the button as a kid of variable, and handle the signal there (again: in File.qml, not Button.qml. The latter file does not know anything about properties of File.qml).[/quote]

    Thanks for the fast reply.

    Somehow I'm completely stuck here.

    How can i catch the buttonClick signal in file.qml?

    I also tried to create a new signal

    signal refreshGroup()

    But how do I send and recieve it?


  • Moderators

    There are many ways to do that. I somehow suspect your design is bad here.

    QML works more or less like C++ class inheritance, signals and slots use Qt's MOC (meta-object compiler) layer. This implies that you have to have some parent, or root object, that knows about both nodes it has to connect (signal sender and receiver). You can go through C++ bridge, but that would probably be a bit too hard at the beginning. Try connecting them in QML. Construct would look somewhat like this:
    @
    // your parent object
    Component.onCompleted: {
    button.buttonClick.connect(variable.mySlot);
    }

    // in File.qml
    Item {
    id: variable
    function mySlot() { console.log("This might work.") }
    }
    @

    You can also take a look at "QML bindings":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html.



  • Okay, as far as I see I don't have any parent object. At least I can't identify it. (it's not my code)

    That's how it is atm:

    Button.qml(as in my first post)
    File.qml (as in my first post)

    MainFile.qml

    @
    PageGrid {
    id: pageGrid
    Button {
    id: feed1
    index: 0
    shortcut: Qt.Key_1
    }
    Button {
    id: feed2
    index: 1
    shortcut: Qt.Key_2
    }
    Button {
    id: feed3
    index: 2
    shortcut: Qt.Key_3
    }
    Button {
    id: feed4
    index: 3
    shortcut: Qt.Key_4
    }
    }
    @

    In MainFile.qml the Buttons are created.

    So, I guess I don't have any object that knows of both nodes (Button & File)?


  • Moderators

    Correct. So, you actually don't have any place in your application, where File.qml is actually instantiated? How do you expect it to work, then?

    I'm sorry, but it's very hard to determine what you actually want to achieve, and what the current situation is - giving advice is now based on guessing on my part.



  • [quote author="sierdzio" date="1345116262"]So, you actually don't have any place in your application, where File.qml is actually instantiated?
    [/quote]

    How would this instantiation look like?

    [quote]
    I'm sorry, but it's very hard to determine what you actually want to achieve, and what the current situation is - giving advice is now based on guessing on my part.[/quote]

    Thanks for your help anyways. I just want this to work! :P

    Again what I want to achieve:

    Button.qml is just a common template file for a button.

    In MainFile.qml I have a Grid in which the Buttons are created. So, whenever I click on one of these buttons I want to change a property that is located in File.qml


  • Moderators

    OK. So, you don't use File.qml at all. In order for any QML component to be instantiated, it has to be incorporated into the parent-child hierarchy.

    So, in your case it goes like this:

    • In c++, you have QDeclarativeViewer::setSource("MainFile.qml");
    • In MainFile, you are using Button.qml, so both have instances now
    • File.qml is not referenced anywhere in those 2 files, so QML engine will not load it.

    Hope that clears this a bit. Now, a somewhat working example would be this:
    @
    // MainFile.qml
    Item {

    File {
    id: myFile
    }

    PageGrid {
    id: pageGrid
    Button {
    id: feed1
    index: 0
    shortcut: Qt.Key_1
    onButtonClick: {
    myFile.group = 1;
    setScreen("MainNewsFeeds")
    }
    }

    Button {
    id: feed2
    index: 1
    shortcut: Qt.Key_2
    onButtonClick: {
    myFile.group = 2;
    setScreen("Bla")
    }
    }
    }
    }
    // File.qml
    Item {
    property int group: 0
    }
    @



  • I actually do use File.qml . Sorry if that wasn't clear.

    If I click on a Button in MainFile.qml I want to change a property AND set the screen to File.qml

    I tried to let MainFile.qml know, that File.qml exists by adding
    @
    File {
    id: myFile
    }
    @

    Setting the screen works, but I can't find access the id variable, which is in the File.qml, but a child of main.


  • Moderators

    id is "protected" in that sense - ids are only valid within QML files. If you import a file, id info is not accessible outside. That is why you can and should use QML properties.



  • [quote author="sierdzio" date="1345119488"]id is "protected" in that sense - ids are only valid within QML files. If you import a file, id info is not accessible outside. That is why you can and should use QML properties.[/quote]

    I still can't change any property of the item with the id variable.


  • Moderators

    Try what I wrote before.

    As I've noticed, properties that are to be exposed to other QML files need to be defined in top-level item in the source file. So in your code, the group property should be defined in "main" Item, not in "variable" item inside File.qml.



  • Adding the properties to the 'main' - item worked!

    Thank you very very much!


  • Moderators

    Uh, finally :) I'm happy you're ok now.

    Best wishes, happy coding!


Log in to reply
 

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