Q_INVOKABLE 메서드에서 반환 된 사용자 지정 QObject가 QML에서 정의되지 않은 이유는 무엇입니까?

Nov 17 2020

QObject에서 파생 된 또 다른 유형 인 MyType의 개체에 대한 const 참조를 반환하는 Q_INVOKABLE 메서드가있는 MasterController QObject가 있습니다. 둘 다 main ()에 등록합니다. MasterController를 인스턴스화하고 main의 루트 컨텍스트에도 추가합니다. 내 QML에서 두 개의 파생 된 QObject 유형을 포함하는 등록 된 모듈을 가져옵니다. QML에서 MasterController 메서드를 호출 할 수 있습니다. 디버거에 들어가는 것을 봅니다. 그러나 실행이 QML 코드로 돌아 가면 반환되는 변수는 '정의되지 않음'입니다. 그래서 나는 그 속성을 읽을 수 없습니다. 사용자 지정 C ++ 유형을 반환 하는 Q_INVOKABLE 메서드에 대한 질문과 답변을 읽었습니다 . 하지만이 문제를 해결하기에 충분한 정보를 제공하지 못했습니다.

MyType.h

#ifndef MYTYPE_H
#define MYTYPE_H

#include <QObject>

#include <testqt-lib_global.h>

namespace testqt {
namespace models {

class TESTQTLIB_EXPORT MyType : public QObject
{
    Q_OBJECT
    Q_PROPERTY( int ui_height READ height )
    Q_PROPERTY( int ui_width READ width )

public:
    explicit MyType(QObject *parent = nullptr);

    int height() const;
    int width() const;

private:
    int _height = 2;
    int _width = 3;
};

} // namespace models
} // namespace testqt

#endif // MYTTYPE_H

MyType.cpp

#include "mytype.h"

namespace testqt {
namespace models {

MyType::MyType(QObject *parent)
    : QObject(parent)
{
}

int MyType::height() const
{
    return _height;
}

int MyType::width() const
{
    return _width;
}

} // namespace models
} // namespace testqt

MasterController.h

#ifndef MASTERCONTROLLER_H
#define MASTERCONTROLLER_H

#include <QObject>

#include "testqt-lib_global.h"
#include "mytype.h"

namespace testqt {
namespace controllers {

class TESTQTLIB_EXPORT MasterController : public QObject
{
    Q_OBJECT

public:
    explicit MasterController(QObject *parent = nullptr);
    ~MasterController();

    Q_INVOKABLE const models::MyType& getData() const;

private:
    models::MyType _myData;
};

} // namespace controllers
} // namespace testqt

#endif // MASTERCONTROLLER_H

MasterController.cpp

#include "mastercontroller.h"

namespace testqt {
namespace controllers {

MasterController::MasterController(QObject *parent)
    : QObject(parent)
{
}

MasterController::~MasterController()
{
}

const models::MyType& MasterController::getData() const
{
    return _myData;
}

} // namespace controllers
} // namespace testqt

main.cpp (대부분 상용구)

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "mastercontroller.h"
#include "mytype.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    qmlRegisterType<testqt::models::MyType>("TestQt", 1, 0, "MyType");
    qmlRegisterType<testqt::controllers::MasterController>("TestQt", 1, 0, "MasterController");

    QQmlApplicationEngine engine;

    testqt::controllers::MasterController masterController;
    engine.rootContext()->setContextProperty("masterController", &masterController);

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml

import QtQuick 2.11
import QtQuick.Window 2.11
import TestQt 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("testqt")

    Text {
        id: heightLabel
        anchors.top: parent.top
        anchors.left: parent.left
        text: "height unknown"
    }

    Text {
        id: widthLabel
        anchors.top: heightLabel.bottom
        anchors.left: parent.left
        text: "width unknown"
    }

    Component.onCompleted: {
        var data = masterController.getData();
        if (data)  // data always undefined
        {
            heightLabel.text = data.height.toString();
            widthLabel.text = data.width.toString();
        }
    }
}

답변

1 eyllanesc Nov 17 2020 at 01:58

QObject는 복사 할 수 없으므로 QObject의 참조를 전달할 수없고 포인터를 전달할 수 없습니다.

Q_INVOKABLE QObject* getData();
QObject *MasterController::getData()
{
    return &_myData;
}