Singleton vs uncreatableType
-
Happy New Year!
I wonder which one is (in theory) supposed to be the better solution.
I have a QAbstractListModel that should be re-used in multiple QML Components.
The model will always be the same.Currently I have it registered as a Singleton and use it like this.
... model: MyModelSingleton ...
Would it make a difference in using instead of a Singleton a qmlRegisterUncreatableType?
IIRC the singleton would be able to only be instantiated once in QML (though in the above case is it even instantiated as there is no MyModelSingleton { }? ) ? -
C++ Classes are registered in QML using various functions:
- qmlRegisterType
- qmlRegisterUncreatableType
- qmlRegisterSingleton
2 and 3 are basically the same but 3) can be instantiated once in QML 2) is forbidden to be instantiated.
I want to know a) what is the preferred way b) in the example 3) should also have been instantiated 0 times, right? -
C++ Classes are registered in QML using various functions:
- qmlRegisterType
- qmlRegisterUncreatableType
- qmlRegisterSingleton
2 and 3 are basically the same but 3) can be instantiated once in QML 2) is forbidden to be instantiated.
I want to know a) what is the preferred way b) in the example 3) should also have been instantiated 0 times, right?@damianatorrpm said in Singleton vs uncreatableType:
2 and 3 are basically the same but 3) can be instantiated once in QML 2) is forbidden to be instantiated.
where did you get that information from ?
qmlRegisterUncreatableType is used to register c++ types that you want to know of in qml, for example the class contained enums and you want to use them in QML.
Has 0 to do with singletons
-
@damianatorrpm said in Singleton vs uncreatableType:
2 and 3 are basically the same but 3) can be instantiated once in QML 2) is forbidden to be instantiated.
where did you get that information from ?
qmlRegisterUncreatableType is used to register c++ types that you want to know of in qml, for example the class contained enums and you want to use them in QML.
Has 0 to do with singletons
@J-Hilk That's basically what I said.
What is the difference between a Singleton that you do not instantiate and an uncreatable type?
AFAIK The Instantiation happens when you do Object {}
which the following does not:... model: MyModelSingleton //an QAbstractListModel ...
So should you use for above rather and uncreatable type or a singleton?
-
@J-Hilk That's basically what I said.
What is the difference between a Singleton that you do not instantiate and an uncreatable type?
AFAIK The Instantiation happens when you do Object {}
which the following does not:... model: MyModelSingleton //an QAbstractListModel ...
So should you use for above rather and uncreatable type or a singleton?
@damianatorrpm
maybe this chart will help you more
taken from the documentation from here:
https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html -
To an extend it does. The chart does not compare the advantages of using a singleton vs an uncreatable type if the singleton is not instantiated at all.