0

I have a QML file originally written in Qt5, I am migrating it to the latest QtQuick.Controls 2 version and I am making it compatible with Qt6 too.

I need to keep supporting Qt5 but I couldn't find a way to import Qt5Compat.GraphicalEffects when the app is built with Qt6 and QtGraphicalEffects when Qt5 is used.

Any ideas?

rbrn
  • 35
  • 6
  • In how many files do you import that module? If it is in a few modules you could create 2 .qml where the first one is valid for Qt5 and the other for Qt6 and then you include it in the .pro or CMakeLists.txt depending on the version of Qt. – eyllanesc Oct 17 '21 at 07:33
  • You mean having two versions of my component? – rbrn Oct 17 '21 at 08:31

2 Answers2

1

The following is a fairly hacky workaround, but was enough for me to get it working.

I modified the qml files to import both QtGraphicalEffects and Qt5Compat.GraphicalEffects.

Then, in my initialization C++ code I added

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    qmlRegisterModule("QtGraphicalEffects", 1, 15); // or any other version that you import
#else
    qmlRegisterModule("Qt5Compat.GraphicalEffects", 1, 0);
#endif

Essentially qmlRegisterModule just creates a dummy empty module. Therefore one of the imports works and one does nothing (and which one is the working one depends on the Qt version).


I also tried a different scheme with qmlRegisterModuleImport("QtGraphicalEffects", ...) but this didn't work well, possibly because my empty QtGraphicalEffects module had no qmldir, I'm not completely sure here... This would be cleaner (let you keep the QML code the same) but it seems to be a dead end.

DavidW
  • 29,336
  • 6
  • 55
  • 86
0

Unfortunately, QML doesn't have anything like this:

#ifdef QT6 
  import Qt5Compat.GraphicalEffects
#else
  import QtGraphicalEffects
#endif

So the best alternative I have found is to use QQmlFileSelector. It will require two versions of your component, but your application doesn't need to know that there are two versions. The right version will be selected automatically.

You can add your selector (like "qt6"), and then create your qml files in a file structure like this:

qml/MyComponent.qml
qml/+qt6/MyComponent.qml
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • This is so sad :(, I will try to generate those qml files from a single version using a script or something – rbrn Oct 18 '21 at 18:39