0

I'm trying to run this example with qt 5.15.1

When I declare QML_IMPORT_NAME, the variable seems to be unknown from qt (see the font's color on the below screenshot) and when I import "com.mycompany.messaging" in my qml file, I have an error "QML module not found."

Edit: After some investigations, the code runs as it should but I have this error in Qt Creator. If I want to edit qml file with the gui editor, I need to comment out all code related to the backend in text mode before otherwise, it fails to open the file.

What is the trick?

enter image description here

8
  • Did you add the QML_ELEMENT (or similar) macro to a C++ type? Commented Sep 28, 2020 at 10:37
  • Yes. : class UiBackend : public QObject { Q_OBJECT Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) QML_ELEMENT public: Commented Sep 28, 2020 at 11:12
  • The example works for me. You said you're using "qt 15.1", which I assume is a typo and that you're actually using 5.15.1? Did you run qmake and rebuild after making the changes? Commented Sep 28, 2020 at 12:46
  • Yes, right 5.15.1, I did qmake and rebuild many times.... :( Commented Sep 28, 2020 at 15:56
  • Can you tell me if QML_IMPORT_CONFIG is coloured in your pro file ? Commented Sep 28, 2020 at 15:59

2 Answers 2

1

With this, I assumed I should have added

CONFIG += qmltypes

to .pro file. But since, I switched to cmake and did not find an equivalent, so I used the old method with:

qmlRegisterType<Person>("People", 1,0, "Person");

in main.cpp (see above link).

Sign up to request clarification or add additional context in comments.

Comments

1

This is a known bug that is not fixed yet (https://bugreports.qt.io/browse/QTCREATORBUG-24987).

The reason for the error is that QtCreator requires the generated .qmltypes and metatypes.json files next to the application binary.

A workaround for this is to add the following to your pro file:

CONFIG(debug, debug|release) { 
    EXE_DIR = $${OUT_PWD}/debug
} else { 
    EXE_DIR = $${OUT_PWD}/release
} 

CONFIG += file_copies 
COPIES += qmltypes metatypes 

qmltypes.files = $$files($${OUT_PWD}/$${TARGET}.qmltypes)
qmltypes.path = $${EXE_DIR} 

metatypes.files = $$files($${OUT_PWD}/$${TARGET}_metatypes.json)
metatypes.path = $${EXE_DIR}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.