Question

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
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top