Unsolved Conditional Import
-
I'm trying to use loader to do something like:
import yada 1.0 import whatever 2.0 Loader { source: condition ? "import.qml" : "" } Rectangle { ...
where import.qml contains only:
import MyQml 1.0
I'm receiving a expected token 'numeric literal' syntax error. Any ideas to resolve this error or a conditional import example to share?
Thanks,
-Rich -
@rhb327 Please put the real code and not pseudo code, also indicate what the .qml names are. For example in your first code there are 2 roots(Loader and Rectangle). In problems where a syntax error is pointed out then the real code is necessary.
-
QML is declarative, you can't do such things. If you want to achieve some dynamic behaviour for your scene you can use
Loader
orComponent
.
You can also tryqmlRegisterModule
which allows imports without any registered types inside. -
Actual source snippets...
import QtQuick 2.0 import MyController 1.0 Loader { source: !RUN_ON_WINDOWS ? "wifi_single.qml" : "" } CustomController{ property bool fwUpdtProgress: false property bool autoFwUpdate: false ...
And wifi_single.qml:
import Wifi.Singleton 1.0
RUN_ON_WINDOWS is defined in another module as:
#ifdef Q_OS_WIN viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(true)); #else viewer.rootContext()->setContextProperty("RUN_ON_WINDOWS", QVariant(false)); #endif
Error is:
wifi_single.qml:2: error: Expected token `numeric literal'
Thanks,
Rich -
@rhb327
better use the following in QML:Qt.platform.os === "windows"
And wifi_single.qml:
import Wifi.Singleton 1.0
so your "wifi_single.qml" just contains the import statement?!
-
I don't seem to be able to use if(Qt.platform.os === "windows") for conditional imports. This only works in code regions from what I can tell.
if(RUN_ON_WINDOWS) also works in code regions but not for conditional imports.
Thanks,
-Rich -
@rhb327
i proposed this to use instead of your context property approach.
conditional imports are jsut not possible in QML.You can use Loader to load components/QML-files which use 2 different imports.
But to me it rather seems that your issue lies somewhere else. Why do you need 2 different imports for different platforms in the first place?
-
import QtGraphicalEffects 1.0 // Required by Qt 5.x.x
vs
import Qt5Compat.GraphicalEffects // Required by Qt 6.x.x -
This post is deleted! -
@caicx said in Conditional Import:
import QtGraphicalEffects 1.0 // Required by Qt 5.x.x
vs
import Qt5Compat.GraphicalEffects // Required by Qt 6.x.xyes and?
Since the major version of Qt has changed you should also advance with your project, instead of importing a compat module of a previous version for the whole lifespan of Qt 6.
The compat module is intended to be used as a quick solution until the porting of your application to Qt6 has finished