PDA

View Full Version : Common import for all qml files



volcano
12th August 2016, 03:14
Hi all,

First.qml


import QtQuick 2.3
import QtQuick.Controls 1.4


Second.qml


import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1


I have a lot of qml files and each file has different imports. Is it possible to have them in one location/file and import that location/file in all the qml files?

anda_skoa
12th August 2016, 10:56
No, since there is no include mechanism that works like a code replacement preprocessor.

One could potentially implement something like that using the QQmlEngine's URL interceptor facility or generate combined files during the application's build step.

Cheers,
_

volcano
12th August 2016, 11:36
Hi anda_skoa,

Could you provide a example of how to use QQmlEngine's URL interceptor facility?

Kindly advice

anda_skoa
12th August 2016, 15:27
You derive from https://code.woboq.org/qt5/qtdeclarative/src/qml/qml/qqmlabstracturlinterceptor.h.html#QQmlAbstractUrlI nterceptor and then set it on the QQmlEngine object https://code.woboq.org/qt5/qtdeclarative/src/qml/qml/qqmlengine.cpp.html#_ZN10QQmlEngine17setUrlInterce ptorEP26QQmlAbstractUrlInterceptor

All URLs used by the engine, QML files, images, JavaScript file, pass through the interceptor.

You could use this to get the actually requested file, generate a new file that has the common imports and the rest of the original file and then return the URL to this file.

Cheers,
_