Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]Accessing variables of other QML files
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Accessing variables of other QML files

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 2 Posters 13.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Al0x
    wrote on last edited by
    #5

    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)?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #6

      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.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Al0x
        wrote on last edited by
        #7

        [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

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #8

          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
          }
          @

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Al0x
            wrote on last edited by
            #9

            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.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #10

              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.

              (Z(:^

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Al0x
                wrote on last edited by
                #11

                [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.

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #12

                  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.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Al0x
                    wrote on last edited by
                    #13

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

                    Thank you very very much!

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #14

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

                      Best wishes, happy coding!

                      (Z(:^

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved