【问题标题】:Why is the custom QObject returned by my Q_INVOKABLE method undefined in my QML?为什么我的 Q_INVOKABLE 方法返回的自定义 QObject 在我的 QML 中未定义?
【发布时间】:2021-02-27 23:51:20
【问题描述】:

我有一个带有 Q_INVOKABLE 方法的 MasterController QObject,该方法返回对 MyType 对象的 const 引用,这是从 QObject 派生的另一种类型。我在 main() 中都注册了。我实例化一个 MasterController 并将其添加到 main 的根上下文中。在我的 QML 中,我导入了包含两个派生 QObject 类型的注册模块。在 QML 中,我可以调用 MasterController 方法。我在调试器中看到它。但是,当执行返回到 QML 代码时,返回的变量是“未定义的”。所以,我无法阅读它的任何属性。我阅读了Q_INVOKABLE method returning custom C++ type 的问题和答案。但是,它没有为我提供足够的信息来解决这个问题。

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();
        }
    }
}

【问题讨论】:

  • 只是好奇如果您将可调用函数更改为返回指针而不是 const ref 是否有效?

标签: c++ qt qml


【解决方案1】:

QObjects 不可复制,因此您不能传递 QObject 的引用,而是传递指针:

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

【讨论】:

  • 如果我按照您的建议将返回类型更改为(非常量)MyType 指针,那么“var data = masterController.getData();”调用 main.qml 完全失败。 Qt Creator 调试器中的应用程序输出窗口显示“错误:未知方法返回类型:models::MyType*”。
  • 将 getData 返回类型更改为 QObject* 确实有效。我可以发誓我看到了使用自定义类型作为返回类型的示例。但是,我想我不会因为使用 QObject* 而失去任何东西。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2014-06-04
  • 2017-07-23
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多