Home > other >  QT QML C MVVM
QT QML C MVVM

Time:05-14

As far as I know in QT QML is not possible to instantiate a C class in QML Component, unless it is a QQuickItem. I would not like to put all ViewModels on ViewEngine context because it is a very bad pratice create all classes in memory without using.

My question is: How can I instantiate a C ViewModel ou Services/API from a single QML component without usnig ViewEngine Context. Do ViewModels have to be QQuickItem type?

CodePudding user response:

C objects do not need to be QQuickItems, they need to be QObjects. You just need to register your class with the QQmlEngine, like this:

qmlRegisterType<MyObject>("my.component.library", 1, 0, "MyObject");

Then in your qml files, you can instantiate that class like this:

import my.component.library 1.0

MyObject {
    ...
}
  • Related