Qt qml插件c++;使用单例?
我以MyTestPlugin的名义创建了Qt Quick 2Extension插件 在内部,我以MySigLon的名义创建了C++文件,在MySigLon类中添加了QMLL SuntLon。 迈辛格尔顿水电站Qt qml插件c++;使用单例?,qt,qml,Qt,Qml,我以MyTestPlugin的名义创建了Qt Quick 2Extension插件 在内部,我以MySigLon的名义创建了C++文件,在MySigLon类中添加了QMLL SuntLon。 迈辛格尔顿水电站 #ifndef TESTSINGLETON_H #define TESTSINGLETON_H #include <QQuickItem> class TestSingleton : public QQuickItem { Q_OBJECT Q_DISABL
#ifndef TESTSINGLETON_H
#define TESTSINGLETON_H
#include <QQuickItem>
class TestSingleton : public QQuickItem
{
Q_OBJECT
Q_DISABLE_COPY(TestSingleton)
QML_SINGLETON
public:
explicit TestSingleton(QQuickItem *parent = nullptr);
~TestSingleton() override;
signals:
void testSignal();
public:
Q_INVOKABLE int testMethod();
};
#endif // MUSEAGORA_H
在qml import MyTestPlugin 1.0中创建一个新项目,并调用MyTestPlugin.testMethod()
输出Aas如下
<Unknown File>: Registered object must live in the same thread as the engine it was registered with
<Unknown File>: qmlRegisterSingletonType(): "TestSingleton" is not available because the callback function returns a null pointer.
qrc:/main.qml:285: TypeError: Property 'testMethod' of object [object Object] is not a function
:注册的对象必须与其注册的引擎位于同一线程中
:qmlRegisterSingletonType():“TestSingleton”不可用,因为回调函数返回空指针。
qrc:/main.qml:285:TypeError:对象[object object]的属性“testMethod”不是函数
可以使用C++单线程吗?
我该如何正确使用?我自己找到了方法
static QObject *test_singleton_type_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
TestSingleton *e = new TestSingleton();
return e;
}
void MuseAgoraPlugin::registerTypes(const char *uri)
{
// @uri TestSingleton
qmlRegisterSingletonType< TestSingleton >(uri, 1, 0, "TestSingleton", test_singleton_type_provider);
}
静态QObject*测试\单例\类型\提供程序(QQmlEngine*引擎、QJSEngine*脚本引擎)
{
Q_未使用(发动机)
Q_未使用(脚本引擎)
TestSingleton*e=新的TestSingleton();
返回e;
}
void MuseAgoraPlugin::registerTypes(常量字符*uri)
{
//@uritestsingleton
qmlRegisterSingletonType(uri,1,0,“TestSingleton”,test\u singleton\u type\u提供程序);
}
<Unknown File>: Registered object must live in the same thread as the engine it was registered with
<Unknown File>: qmlRegisterSingletonType(): "TestSingleton" is not available because the callback function returns a null pointer.
qrc:/main.qml:285: TypeError: Property 'testMethod' of object [object Object] is not a function
static QObject *test_singleton_type_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
TestSingleton *e = new TestSingleton();
return e;
}
void MuseAgoraPlugin::registerTypes(const char *uri)
{
// @uri TestSingleton
qmlRegisterSingletonType< TestSingleton >(uri, 1, 0, "TestSingleton", test_singleton_type_provider);
}