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. Loading component from other QML file
QtWS25 Last Chance

Loading component from other QML file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    mleoni
    wrote on last edited by
    #1

    Hello everyone, I am trying to load a component from an auxiliary qml file and I can't find the correct way to do it. I know it's a very basic question, but I'm really a beginner with QML.

    The relevant part of my code is

    StackView {
        // Stuff
        initialItem: ListView {
            // Stuff
            delegate: ItemDelegate
            {
                // Stuff
                contentItem: Rectangle
                {
                    // Stuff
                }
    

    Now, I want to move the Rectangle to a separate file and replace its implementation in the code above with a suitable "call".

    I tried to naïvely copy-paste the Rectangle {} part to a new file myItem.qml and then replace it, in the code above, with contentItem: "myItem.qml" but I get the error

    Invalid property assignment: unsupported type "QQuickItem*"
    

    I tried various [pointless] things such as wrapping everything inside an Item {} [same error] or calling contentItem: "myItem" [yields ReferenceError for object not defined] but to no avail.

    Can anybody point me in the right direction? I feel I should be pretty close since it's a dumb task.

    Thank you!

    1 Reply Last reply
    0
    • KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      Hi there, I have a question.

      • the myItem.qml is in same prefix of main.qml ? (relative of resource.qrc file)

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mleoni
        wrote on last edited by
        #3

        Hi! My qml files are in a qml/ subfolder. My qml.qrc file reads

        <RCC>
            <qresource prefix="/">
                <file>qml/main.qml</file>
                <file>qml/buysItem.qml</file>
            </qresource>
        </RCC>
        

        the project file contains

        RESOURCES += qml.qrc
        

        and in the code I actually wrote contentItem: "qml/myItem.qml". Does it help?

        1 Reply Last reply
        0
        • KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #4

          Sorry for delay.
          To solve this problem, you need to do:

          • Change the first letter of your component to Uppercase (because Qt Name Rules), so, you need to rename the file buysItem.qml to BuysItem.qml.

          • Replace contentItem: "qml/myItem.qml" to contentItem: BuysItem{}

          Obs: All variables begin with lowercase letter and components with uppercase letter.

          Read More:
          http://doc.qt.io/qt-5/qtqml-syntax-directoryimports.html
          http://doc.qt.io/archives/qt-4.8/qmlreusablecomponents.html

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mleoni
            wrote on last edited by
            #5

            Ah, that fixed it! Thank you very much :D

            aha_1980A 1 Reply Last reply
            1
            • M mleoni

              Ah, that fixed it! Thank you very much :D

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mleoni Then please close this topic as SOLVED (button Topic Tools below your first post). Thanks

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mleoni
                wrote on last edited by
                #7

                Done, thanks for reminding me!

                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