QML Module not found with registered types

Viewed 682

I have the following Issue:

In main.qml I get these errors. Although I can use these types perfectly in the code. It looks like it is just an intellisense issue.

f5bb7882-0104-47bb-be23-e2d160bc7c2c-image.png

These types are registered in main.cpp:

e745d480-b943-490b-9bba-f253dc8f4974-image.png

Thse classes are defined in the include folder:

My folder structure looks like this:

a4997bc8-5c83-459d-85e0-de8fb6eade18-image.png

Do I have to modify QML_IMPORT_PATH in the pro file? I added src and include folder but it does not work:

QML_IMPORT_PATH += src
QML_IMPORT_PATH += include

The code itself runs fine. It is just an Intellisense issue.

2 Answers

I assume this is simply a Qt Creator bug. Take a look at this one. qmlRegisterSingletonInstance was added to the Qt library in version Qt 5.14. Even though Qt Creator 4.13.3 was built with Qt 5.15.2, the QML code model it uses has apparently still not been updated.

You need to run this code. QtCreator is notorious for flagging errors that don't exist or won't exist. It flags header files for .ui files because you haven't run a build yet so they haven't been generated. Many developers paint their UI files then do a fake build just to generate those files so QtCreator shuts up.

The other thing you need to do is provide the full source code for one of those classes. (I will assume they all have the same issue.)

The example Qt gives here isn't a good one. You should never be able to "new" a Singleton. The constructor should be protected and the Instance() method should construct one and only one if the internal pointer is null.

Do you actually have a method named get() in each of those classes? Does it actually return a pointer? Many return a reference, that is why I'm asking. If we overlook the glaring error of being able to "new" a Singleton, there is one good thing in this example.

QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);

They used a QScopedPointer to the class.

qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());

Once you actually build you will have all of the MOC information where it needs to be. This may well make QtCreator happy. Honestly, I've stopped looking at what QtCreator flags anymore because there are so many false alarms.

Related