Can't connect cpp to qml project
-
Good day to all,
I'm having trouble connecting C++ to QML in a Qt for MCU project. The main issue is that QML seems to not recognize my C++ classes or structures.
Despite numerous attempts, I was unable to find and install Qt Ultralite via the online installer. I'm uncertain if this could be the root of the problem, or if it's something else entirely.
I'm using STM32F769I-Discovery board and tried to download it's kit also.
In my project https://github.com/Funkvay/QtMCU, I have a rather simple setup, and I've tried various approaches to make it work. For example, I have tried using a singleton structure as well as a regular Qul::Object.
In QML, I have code like this:
Item { Component.onCompleted: test1.feed(5); }
and
test1 { id: test1 bigness: 61 onGotFood: bigness = 99 }
These are just some attempts to see if QML would recognize my C++ objects (see the image.png in the gitub to see the other version of "test1").
Here I get errors like
MCU2Depricated.qml:20: error: Could not determine type for global name test1 Component.onCompleted: test1.feed(5);
or
Invalid property name "test1"
However, despite these efforts, QML seems unable to see my C++ entities. At this point, I'm unsure what could be causing this issue. Could it be something in CMake, or perhaps an incorrect declaration of the class, or could the absence of Ultralite be the problem?
I tried to create new projects to try this out, but it didn't work.
I'd greatly appreciate any guidance or suggestions on what to try next. If more information is needed to diagnose the issue, please let me know.
Thank you in advance.
-
@Armha QML components have to start with a capital letter. So this should be:
Test1 { id: test1 bigness: 61 onGotFood: bigness = 99 }
In order for a type to be visible in QML, you have to:
- make sure the type is a subclass of Q_OBJECT
- make sure it has a public constructor like
MyClass(QObject*parent = nullptr);
- register the type with QML engine https://doc.qt.io/qt-6/qqmlengine.html#qmlRegisterType
- in QML file, import the URI you provided to
qmlRegisterType
Then it should work :-)
A thing to remember: When you use the
Test1 { somtething }
syntax in QML, you are creating an instance of an object (in this case, of your C++ class). So adding a singleton of the same class to the project won't help here at all. -
@sierdzio Thank you very much for the answer. I tried some tips, but unfortunately not all were applicable in my case, since let's say the same Q_OBJECT is missing in Qt microcontrollers. And in the examples that I'm looking at, it seems that there is no such thing (Q_OBJECT), which is why I ran into a problem, since everything works in the usual Qt for Desktop but not in Qt MCU