【发布时间】: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 是否有效?