QML parsing errors, from not included qml files
-
So I have a "QML library" that is used in different projects, those themselves can be compiled with Qt5 or Qt6.
For that reason I was forced to split some files into Qt5 and Qt6 files and load them dynamically via a Loader component. Annoying but not unreasonable.
Compiling for Qt6 works perfectly fine, but for Qt5 I get the following "error":
/Users/.../Qt-Projekte/.../Dependencies/SharedFiles/qml/Dialogs/Base/FileDialogQt6.qml:2: error: package import requires a version number
I could get around it by explicitly giving the Qt6 imports version numbers, but AFAIK it is no longer considered good behaviour to do so in Qt6
so I had the following bright idea:
//pri file to include in other projects RESOURCES ''+= \ $$PWD/img.qrc \ $$PWD/sharedQml.qrc contains(QT_MAJOR_VERSION, 5) { RESOURCES += $$PWD/sharedQmlQt5.qrc } else { RESOURCES += $$PWD/sharedQmlQt6.qrc }
//sharedQmlQt5.qrc <RCC> <qresource prefix="/Dependencies/SharedFiles"> <file>qml/Dialogs/Base/FileDialogQt5.qml</file> <file>qml/Dialogs/Base/FolderDialogQt5.qml</file> <file>qml/Dialogs/Base/MessageDialogQt5.qml</file> <file>qml/Dialogs/Base/SaveFileDialogQt5.qml</file> </qresource> </RCC>
//sharedQmlQt6.qrc <RCC> <qresource prefix="/Dependencies/SharedFiles"> <file>qml/Dialogs/Base/FileDialogQt6.qml</file> <file>qml/Dialogs/Base/FolderDialogQt6.qml</file> <file>qml/Dialogs/Base/MessageDialogQt6.qml</file> </qresource> </RCC>
The
sharedQmlQt6.qrc
is greyed out inside QtCreator, but theerror: package import requires a version number
does not disappear.Compiling and running is completely fine. But the error-tab has errors in it.
commenting the rsc out, works but is not a robust and elegant solution either.
RESOURCES ''+= \ $$PWD/img.qrc \ $$PWD/sharedQml.qrc contains(QT_MAJOR_VERSION, 5) { RESOURCES += $$PWD/sharedQmlQt5.qrc } else { # RESOURCES += $$PWD/sharedQmlQt6.qrc }
Any suggestions, besides adding the version number in the Qt6 files :D ?
-
@J-Hilk said in QML parsing errors, from not included qml files:
So I have a "QML library" that is used in different projects, those themselves can be compiled with Qt5 or Qt6.
For that reason I was forced to split some files into Qt5 and Qt6 files and load them dynamically via a Loader component. Annoying but not unreasonable.
This sounds like the problem class QFileSelector is intended to solve, even if it doesn't include a Qt version selector automatically. A single #ifdef when setting up the QML engine sounds better to me than multiple loaders.
Alas, I don't know if Creator will recognize non-default (or default) selectors.
-
@jeremy_k interesting! Thank you I knew of QFileSelector, but I have never used it and not thought of it.
In the documentation its nowhere said, that
There's also QQmlFileSelector and QQmlApplicationEngine also comes with an instance backed in with a preset selector (for me in this case) of ("en_DE", "unix", "darwin", "mac", "osx", "macos")I assume I would simply add my qtversion selection:
QQmlApplicationEngine engine; auto qsel = QQmlFileSelector::get(&engine); #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) qsel->setExtraSelectors({"qt6"}); #else qsel->setExtraSelectors({"qt5"}); #endif engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
I could than forgo with the loader, if I adjusted my folder structure
from:- Dialogs - SaveFileDialog.qm - Base - SaveFileDialogQt5.qml //Dialogs/Base/SaveFileDialogQt5.qml - SaveFileDialogQt6.qml //Dialogs/Base/SaveFileDialogQt6.qml
to:
- Dialogs - SaveFileDialog.qm //Default selection, possibly empty? - qt5 - SaveFileDialog.qml //Dialogs/+qt5/SaveFileDialog.qml - qt6 - SaveFileDialog.qml //Dialogs/+qt6/SaveFileDialog.qml
Probably the more elegant solution, and totally what I was originally looking for, before I decided on Loaders! . But I still would need to have all files as part of my resource system qrc:/
<RCC>
<qresource prefix="/Dependencies/SharedFiles">
<file>qml/Dialogs/SaveFileDialog.qml </file>
<file>qml/Dialogs/+qt5/SaveFileDialog.qml</file>
<file>qml/Dialogs/+qt6/SaveFileDialog.qml</file>
</qresource>
</RCC>I don't think this is going to solve my original problem.
Like I said, the "errors" only disappear when I remove them from my project file(*.pro) altogether.But I will try, I think
-
So QQmlFileSelector does work, makes it that much cleaner and easier to use.
But in my case, it's actually worse, previously the Qt6 Qml would cause those error messages in the issue tab, but the program would compile and launch, now it doesn't launch.I tripple checked, the loader works fine, but produces the same errors, but those errors then do not trigger an abort of the compilation.
It does eventually compile, if I explicitly disable the QtQuickCompiler, which is not ideal.
Anyway I'm back to square one :D
Maybe I should open a bug report, the error should not be there, if it's not an actual error. On the other hand, the change to Qt6 is inevitable.