Quick example. It's just my main (shrinked of useless declarations):
@
Application app(argc, argv);
// Viewer object to handle QML types
QtQuick2ControlsApplicationViewer viewer;
// Registering CUSTOM types
qmlRegisterType<ClassName>("ClassName", 1, 0, "QMLNAME");
// Registering SINGLETON CUSTOM types
qmlRegisterSingletonType<AppInfo>("qt.appInfo", 1, 0, "AppInfo", singletonObject);
// Load initial QML file
viewer.setMainQmlFile(QStringLiteral("qml/HS2/main.qml"));
viewer.show();
// main loop starts
return app.exec();
@
In this example I've registered both a singleton class (i.e. a class which is instanced only ONCE in the program) and a non-singleton one. In simple projects you can always set the registration URL (first parameter) equal to the name of the class. Here I've added "qt" as the initial url. In the QML I can import the type and create an object as usual:
@
import ClassName 1.0 // <--- Major/minor == values passed to registration!
QMLNAME {
id: myId
width: ...
height: ...
anchors.centerIn: parent
Component.onCompleted: initFunctionDefinedInTheRegisteredTypeIfAny()
}
@
Have a look to "this page":http://qt-project.org/doc/qt-5/qtqml-cppintegration-definetypes.html for further examples and clarifications! Enjoy! ;)