Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qml

qt5.1

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
like image 361
Horst Walter Avatar asked Oct 23 '13 12:10

Horst Walter


People also ask

What is a QML file?

QML (Qt Modeling Language) is a user interface markup language. It is a declarative language (similar to CSS and JSON) for designing user interface–centric applications. Inline JavaScript code handles imperative aspects.

How do I create a QML file?

Creating and Running QML Projects For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.

How do I import files into QML?

The main/application. qml file can import the mycomponents directory using the relative path to that directory, allowing it to use the QML object types defined within that directory: import "../mycomponents" DialogBox { CheckBox { // ... } Slider { // ... } }

How do I open a QML file?

If you cannot open your QML file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a QML file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

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

like image 99
Wes Hardaker Avatar answered Sep 30 '22 07:09

Wes Hardaker