[英]Why is the custom QObject returned by my Q_INVOKABLE method undefined in my QML?
我有一个带有 Q_INVOKABLE 方法的 MasterController QObject,该方法返回对 MyType 对象的常量引用,这是从 QObject 派生的另一种类型。 我在 main() 中注册了两者。 我实例化了一个 MasterController 并将其添加到 main 中的根上下文中。 在我的 QML 中,我导入了包含两个派生 QObject 类型的注册模块。 在 QML 中,我可以调用 MasterController 方法。 我看到它在调试器中。 但是,当执行返回到 QML 代码时,返回的变量是“未定义”。 所以,我无法阅读它的任何属性。 我阅读了Q_INVOKABLE 方法返回自定义 C++ 类型的问题和答案。 但是,它没有给我足够的信息来做对。
我的类型文件
#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
我的类型文件
#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
主控制器.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
主控制器.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();
}
主文件
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();
}
}
}
QObjects 不可复制,因此您不能传递 QObject 的引用,而是传递指针:
Q_INVOKABLE QObject* getData();
QObject *MasterController::getData()
{
return &_myData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.