Qt Quick2 为 Singleton Class 类型创建 qmlRegisterSingletonType
Qt Quick2 Creating qmlRegisterSingletonType for Singleton Class Type
我正在尝试使用 qmlRegisterSingletonType
创建单例 属性 但是当我尝试访问 QML 中的对象时,控制台日志中出现以下错误:
qrc:/qml/MyQml.qml:21 Element is not creatable.
下面是我的代码:
// TestSingletonType.h Class
#include <QObject>
#include <QJsonObject>
#include <QVariantMap>
#include <QQmlEngine>
class TestSingletonType : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(TestSingletonType)
TestSingletonType(QObject *parent = nullptr) {}
public:
// To maintain single object of the class
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
if (theInstance == NULL)
theInstance = new TestSingletonType();
return theInstance;
}
private:
static QObject *theInstance; // I have set it to NULL in Cpp file
};
// Main.cpp
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterSingletonType<TestSingletonType>("com.my.TestSingletonType", 1, 0, "TestSingletonType", &TestSingletonType::qmlInstance);
// Rest of the code to load the QML
return app.exec();
}
// MyQml.qml 文件:
import QtQuick 2.0
import QtQuick.Controls 2.0
import com.my.TestSingletonType 1.0
Item {
TestSingletonType { <---- Getting error on this line
id: mySingleClass
}
// Rest of my code which uses "mySingleClass"
}
如果我使用 qmlRegisterType
则它可以正常工作,但不能与 qmlRegisterSingletonType
一起使用。
我参考了下面的答案 link:
How to implement a singleton provider for qmlRegisterSingletonType?
https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType
错误很明显:单例未在 QML 中创建,因为它已在回调(qmlInstance 方法)中创建,您只需访问该方法的属性即可。 typeName ("TestSingletonType") 是 id。
Item {
// binding property
foo_property : TestSingletonType.foo_value
// update singleton property
onAnotherPropertyChanged: TestSingletonType.another_prop = anotherProperty
}
// listen singleton signal
Connections{
target: TestSingletonType
onBarChanged: bar_object.bar_property = bar
}
我正在尝试使用 qmlRegisterSingletonType
创建单例 属性 但是当我尝试访问 QML 中的对象时,控制台日志中出现以下错误:
qrc:/qml/MyQml.qml:21 Element is not creatable.
下面是我的代码:
// TestSingletonType.h Class
#include <QObject>
#include <QJsonObject>
#include <QVariantMap>
#include <QQmlEngine>
class TestSingletonType : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(TestSingletonType)
TestSingletonType(QObject *parent = nullptr) {}
public:
// To maintain single object of the class
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
if (theInstance == NULL)
theInstance = new TestSingletonType();
return theInstance;
}
private:
static QObject *theInstance; // I have set it to NULL in Cpp file
};
// Main.cpp
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterSingletonType<TestSingletonType>("com.my.TestSingletonType", 1, 0, "TestSingletonType", &TestSingletonType::qmlInstance);
// Rest of the code to load the QML
return app.exec();
}
// MyQml.qml 文件:
import QtQuick 2.0
import QtQuick.Controls 2.0
import com.my.TestSingletonType 1.0
Item {
TestSingletonType { <---- Getting error on this line
id: mySingleClass
}
// Rest of my code which uses "mySingleClass"
}
如果我使用 qmlRegisterType
则它可以正常工作,但不能与 qmlRegisterSingletonType
一起使用。
我参考了下面的答案 link:
How to implement a singleton provider for qmlRegisterSingletonType?
https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType
错误很明显:单例未在 QML 中创建,因为它已在回调(qmlInstance 方法)中创建,您只需访问该方法的属性即可。 typeName ("TestSingletonType") 是 id。
Item {
// binding property
foo_property : TestSingletonType.foo_value
// update singleton property
onAnotherPropertyChanged: TestSingletonType.another_prop = anotherProperty
}
// listen singleton signal
Connections{
target: TestSingletonType
onBarChanged: bar_object.bar_property = bar
}