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

≡放荡痞女 提交于 2019-12-23 12:15:49

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/19541369/qml-file-include-or-one-monolithic-file-structure-qml-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!