main.cpp中,我正在创建一个单例类型的类,以通过以下功能在qml中使用它:

qmlRegisterSingletonType<DataloopWrapper>("com.xpto.connector", 1, 0, "DataloopWrapper",&DataloopWrapper::qmlInstance);

有点向下main.cpp我调用另一个对象的函数,其中我想打电话从singleton的函数

也许是这样的qmlTranslator.loadLanguage(lang, DataloopWrapper::qmlInstance);是可能的?

我声明了QmlTranslator类方式如下:

#ifndef QMLTRANSLATOR_H
#define QMLTRANSLATOR_H

#include <QObject>
#include <QTranslator>
#include <QQmlEngine>
#include <QGuiApplication>

class QmlTranslator : public QObject
{
    Q_OBJECT

public:
    QmlTranslator(QQmlEngine *engine, QGuiApplication *app);
    Q_INVOKABLE void selectLanguage(QString language);
    void InstallTranslator();
    void loadLanguage(QString language, QObject*(QQmlEngine*,QJSEngine*) objDataloop);
    virtual ~QmlTranslator();
signals:
    void languageChanged();

private:
    QTranslator *_translator;
    QQmlEngine *_engine;
    QGuiApplication *_app;
};


#endif // QMLTRANSLATOR_H


我没有找到一种方法来通过DataloopWrapper::qmlInstance作为参数。至少它在qmlTranslator定义的void loadLanguage(QString language, QObject*(QQmlEngine*,QJSEngine*) objDataloop);中给出错误。

从市民:成员DataloopWrapper我的qmlInstance,它是这个defenition:

class DataloopWrapper : public QObject, public something::DataloopCBHandler,
                        public something::DataloopTransferCBHandler
{
    Q_OBJECT
public:
    explicit DataloopWrapper(QObject *parent = nullptr);
    virtual ~DataloopWrapper();
    static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        Q_UNUSED(engine);
        Q_UNUSED(scriptEngine);

        return new DataloopWrapper;
    }

最佳答案

DataloopWrapper::qmlInstance是一个函数,你需要调用,以获得一个指向QObject

对于要传递此对象的任何函数,都需要使它接受指向QObject的指针(类型为QObject*),并且不要忘记调用DataloopWrapper::qmlInstance



翻译成代码:

class QmlTranslator : public QObject
{
    ...
    void loadLanguage(QString language, QObject* objDataloop);
    ...
};

...

qmlTranslator.loadLanguage(lang, DataloopWrapper::qmlInstance(aQMLEngineObjectPointer, aQJSEnginePointer));

10-01 08:22