I am trying to use a non-static method as callback method for qmlRegisterSingletonType in Qt 5. My code looks like this:
PersistentLong couponCounter("couponCounter", handler.getSubjectRunner(), handler.getDbManager());
qmlRegisterSingletonType<PersistentLong>("MyNamespace", 1, 0, "CouponCounter", couponCounter.factory);
But I get the following compiler error:
/.../src/Main.cxx: In function ‘int main(int, char**)’:
/.../src/Main.cxx:22:102: error: no matching function for call to ‘qmlRegisterSingletonType(const char [9], int, int, const char [14], <unresolved overloaded function type>)’
qmlRegisterSingletonType<PersistentLong>("MyNamespace", 1, 0, "CouponCounter", couponCounter.factory);
^
/.../src/Main.cxx:22:102: note: candidate is:
In file included from /usr/local/Qt-5.3.2/include/QtQml/QtQml:9:0,
from /.../src/Main.cxx:6:
/usr/local/Qt-5.3.2/include/QtQml/qqml.h:476:12: note: template<class T> int qmlRegisterSingletonType(const char*, int, int, const char*, QObject* (*)(QQmlEngine*, QJSEngine*))
inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
^
/usr/local/Qt-5.3.2/include/QtQml/qqml.h:476:12: note: template argument deduction/substitution failed:
/.../src/Main.cxx:22:102: note: cannot convert ‘couponCounter.PersistentLong::factory’ (type ‘<unresolved overloaded function type>’) to type ‘QObject* (*)(QQmlEngine*, QJSEngine*)’
qmlRegisterSingletonType<PersistentLong>("MyNamespace", 1, 0, "CouponCounter", couponCounter.factory);
The factory method of PersistentLong looks like this:
QObject * factory(QQmlEngine *engine, QJSEngine *scriptEngine) {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
return this;
}
Questions
Any ideas as to why I can't use this method as argument to qmlRegisterSingletonType?
Is there another way to register a non-static instance as a singleton in QML?
Edit 1
couponCounter is constructed in main() in the following way:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtSubjectHandler handler("burgundy_frontend");
PersistentLong couponCounter("couponCounter", handler.getSubjectRunner(), handler.getDbManager());
qmlRegisterSingletonType<PersistentLong>("MyNamespace", 1, 0, "CouponCounter", couponCounter.factory);
}
If I use a function pointer, how can I reference couponCounter in this function?