Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
6.4.2
-
None
Description
https://doc.qt.io/qt-6/qqmlengine.html#QML_SINGLETON
the example using create function is missing an important piece.
the create function only be triggered when the default constructor is detected.
but the code example makes the user missing understand and when try to run it the create function will never get called and without any error or warning, it requires the user to delete the constructor like
class MySingleton : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON // Q_PROPERTY( ... ) public: MySingleton (QObject* object) { // replace the default constructor to trigger create function } static MySingleton *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) { MySingleton *result = nullptr; // Create the object using some custom constructor or factory. // The QML engine will assume ownership and delete it, eventually. return result; } // members, Q_INVOKABLE functions, etc };
either fix the example code or suggest changing the specification if create is defined call it first instead of the constructor.