rbrn
rbrn

Reputation: 55

Use Qt5Compat.GraphicalEffects in Qt6 and QtGraphicalEffects in Qt5 in the same QML file

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?

Upvotes: 1

Views: 2208

Answers (2)

DavidW
DavidW

Reputation: 30911

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.

Upvotes: 3

JarMan
JarMan

Reputation: 8277

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

Upvotes: 1

Related Questions