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] General scope question
QtWS25 Last Chance

[SOLVED] General scope question

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.6k Views
  • 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.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by
    #1

    Hi,
    as I'm a totaly unexperienced QML scripter, I need to know some general things about scopes.

    Let's say I have a main.qml (which is loaded in the c++ source file) and about 5 qml components (CompA.qml, CompB.qml, CompC.qml, CompD.qml, CompE.qml).
    They are all stored in the same directory as main.qml.

    When I try to use a component in my main.qml, I'm wondering why I don't even have to import/include the component file (e.g. CompA.qml). Instead, I can simply use it like that:
    @//main.qml

    CompA
    {
    //...
    }@

    Question 1:
    Is there a general rule how QtCreator automatically inlcudes QML component files??

    Question 2:
    Let's say, I create 5 instances of CompA in my main.qml, and my CompA has a pre-defined id property. Then I would have 5 objects with the same ID in my main.qml, what should result in an error, right? Instead, I can do that without any problems. So how is the ID - Scope relation handled in QML projects??

    Question 3:
    Let's say, I create 5 instances of CompA with a resulting 5x identically ID, how can I access the component using its ID when it is no longer unique?? (This question links to Question 2 again)?

    Question 4:
    How can I create objects/components that are accessable from two different qml files, e.g. from main.qml and from CompA.qml? Example:
    CompA is a button with a background image. The background image source is a property binding to an Item that should be also accessable from main.qml, because there I want to change the source url and so every button (CompA) should apply these changes immediatelly.
    I don't know how to realize that, because I don't know how to create such an Item that is accessable from main.qml and from CompA.qml (I mean, I can't put the Item into the CompA.qml right? Because then it would be created for each CompA instance, right?)

    I would really appreciate it to get some help from you :-)

    Thank you in anticipation!

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      A *.qml file can access all other *.qml files in the same folder as itself. Also see "QML Import Directories":http://doc.qt.io/qt-5/qtqml-syntax-directoryimports.html

      id is not a property. A QML component can have one id attribute and many property attributes. See "QML Object Attributes":http://doc.qt.io/qt-5.4/qtqml-syntax-objectattributes.html for details.

      In main.qml, you can assign different ids to each instance. main.qml cannot see the id that you used inside CompA.qml.

      If I've understood your requirements correctly, you don't need a separate Item. Just chain multiple property bindings together:

      @
      //main.qml
      Rectangle {
      id: root
      property url buttonImg: "image1.png"

      CompA {
          id: a1
          source: root.buttonImg
      }
      CompA {
          id: a2
          source: root.buttonImg
      }
      
      function updateImages() {
          // Changes the source of all CompA instances
          root.buttonImg = "image2.png"
      }
      

      }
      @

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        Hi and thank's for your reply.

        [quote]In main.qml, you can assign different ids to each instance. main.qml cannot see the id that you used inside CompA.qml.[/quote]So the id scope is limited to what? I mean, in the CompA.qml, I can access every object by its ID, so are IDs limited to its *.qml file?

        [quote]If I’ve understood your requirements correctly, you don’t need a separate Item. Just chain multiple property bindings together:[/quote]
        Oh yeah that fits the problem!! Thank you very much!
        But maybe just for learning a little more, how would I have to do it with the global item that is accessable from different files? Do you know that?

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          You're most welcome :)

          [quote author="Binary91" date="1419542117"]So the id scope is limited to what? I mean, in the CompA.qml, I can access every object by its ID, so are IDs limited to its *.qml file?[/quote]In simple terms, yes you're right. To be more precise though, an ID is limited to "Component Scope":http://doc.qt.io/qt-5/qtqml-documents-scope.html#component-scope

          [quote author="Binary91" date="1419542117"]But maybe just for learning a little more, how would I have to do it with the global item that is accessable from different files? Do you know that?[/quote]That's not possible. You cannot create a global item in QML.

          Anyway, can you think of a good use-case where you need to have a global item?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on last edited by
            #5

            [quote]In simple terms, yes you’re right. To be more precise though, an ID is limited to Component Scope [doc.qt.io][/quote]Ahh.. ok that sounds interesting. So items in the main.qml have global ids but the other way round, it is not possible.

            [quote]That’s not possible. You cannot create a global item in QML.

            Anyway, can you think of a good use-case where you need to have a global item?[/quote]Hmm, I think I'm lacking in required experience to answer this question, but the way you ask tells me the answer :-P

            Btw.: On my extensive investigation using google I found another way that could also be useful. When I use a singleton, I can make the component accessable from other files (if I understood that right), but I don't think that this is the right way for my purposes, because singleton is "more" useful for components which are created lots of times to save memory/performance...

            Well, thank you for spending a lot of time explaining that stuff to me, I appreciate that much! :-)

            Cheers,

            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