我创建了一个名为LoginService的类。 我使用qmlRegisterSingletonType将它注册到QT QML文件,现在问题是我无法获得由QML实例化的loginservice实例。我目前的c ++代码是:
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
LoginService::m_pThis = new LoginService;
return m_pThis;
}
qmlRegisterSingletonType<LoginService>("com.test.LoginService", 1, 0, "LoginService", &LoginService::qmlInstance);
答案 0 :(得分:2)
如果要从C ++访问单例,请创建一个返回qmlInstance
以外的实例的方法:
<强> loginservice.h 强>
#ifndef LOGINSERVICE_H
#define LOGINSERVICE_H
#include <QObject>
class QQmlEngine;
class QJSEngine;
class LoginService : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
static LoginService *instance();
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
QString name() const;
void setName(const QString &name);
Q_SIGNAL void nameChanged();
private:
explicit LoginService(QObject *parent = nullptr);
static LoginService* m_pThis;
QString mName;
};
#endif // LOGINSERVICE_H
<强> loginservice.cpp 强>
#include "loginservice.h"
#include <QQmlEngine>
LoginService* LoginService::m_pThis = nullptr;
LoginService::LoginService(QObject *parent) : QObject(parent)
{
}
QString LoginService::name() const
{
return mName;
}
void LoginService::setName(const QString &name)
{
if(mName == name)
return;
mName = name;
Q_EMIT nameChanged();
}
LoginService *LoginService::instance()
{
if (m_pThis == nullptr) // avoid creation of new instances
m_pThis = new LoginService;
return m_pThis;
}
QObject *LoginService::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
// C++ and QML instance they are the same instance
return LoginService::instance();
}
<强>的main.cpp 强>
#include "loginservice.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterSingletonType<LoginService>("com.test.LoginService", 1, 0, "LoginService", &LoginService::qmlInstance);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
// get instance in C++
LoginService *service = LoginService::instance();
qDebug()<<service->name();
return app.exec();
}
<强> main.qml 强>
import QtQuick 2.9
import QtQuick.Window 2.2
import com.test.LoginService 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Component.onCompleted: LoginService.name = "testing"
}