QML file include - or one monolithic file (structure QML code)?
Asked Answered
U

1

6

This is a QML newbie question. From the table view example I have code like this:

Column {
        anchors.top: toolbar.bottom
        .....

        TabView {
            id:frame
            ......

            Tab {
                title: "XmlListModel"
                ...
            }
            Tab { ...

As the qml file gets quite long, I wonder if I can have nested qml files

Column {
        anchors.top: toolbar.bottom
        .....

        TabView {
            id:frame
            ......

            <include tab 1 qml file>   <-- possible ????? -------
            <include tab 2 qml file>

If such an include is not possible, how does a QML programmer structure his code? Even in the simple example there are already far too many lines to handle IMHO.

-- Edit --

After the answer I have found this readworthy:

  1. http://qt-project.org/doc/qt-5.0/qtqml/qtqml-syntax-directoryimports.html
  2. How to reuse code at QML
Unattached answered 23/10, 2013 at 12:13 Comment(0)
S
6

No, you can't do "includes" but you can put things into named objects.

For example, take your Tab #1 file, put it in a file called "Tab1" (or a better name that relates to what it's actually displaying; I don't know so can't help you with a name).

So in Tab1.qml we have:

import ...
Tab {
  id: tab1
  ...
}

And then in the main file you can now reference it:

...
Tabview {
   id: frame
   Tab1 { id: tab1 }
}

You'll note that I included an id for it again, as the parent won't be able to reference the id within the child without it. (they can be different names, but don't do that. Animals will cry. Actually, you can leave out the id in the child as well, but many people like being able to see it within a file.)

Seicento answered 23/10, 2013 at 13:24 Comment(1)
If it didn't, it's likely do to a problem with your code that you're not seeing. Maybe you should post it as another question?Seicento

© 2022 - 2024 — McMap. All rights reserved.