Create custom QML for QT

Create a QML can be import by other QML

description

to avoid a QML file too height, and create re-usable module, I introduction 2 methods in here:

notice

there are some points and both of built-in and rcc file are almost the same:

How to do

Built-in

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
  Rectangle {
    anchors.fill: parent
    ...
  }
}
ExtendObj 1.0 extend.qml
<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
    <qresource prefix="/modules">
        <file>extend.qml</file>
        <file>qmldir</file>
    </qresource>
</RCC>
import QtQuick 2.0
import QtQuick.Window 2.2
import "qrc:/modules"

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ExtendObj {
        anchors.fill: parent
        ...
    }
}

now, you can use the QML module that is built by yourself.

external RCC filo

we only adjust some procedure from built-in methods (still use extend.qml and qmldir)

<RCC>
    <qresource prefix="/modules">
        <file>extend.qml</file>
        <file>qmldir</file>
    </qresource>
</RCC>
rcc -binary common.qrc -o common.rcc
#include <QResource>
...
...
QResource::registerResource("common.rcc")

and you can use the same method to call your modules in main.qml (or other QML files).