C++ 为什么我的Q_可调用方法返回的自定义QObject在我的QML中没有定义?
我有一个MasterController QObject,它带有一个Q_可调用方法,该方法返回对MyType对象的常量引用,MyType是从QObject派生的另一种类型。我在main()中注册这两个。我实例化了一个MasterController,并将其添加到main中的根上下文中。在我的QML中,我导入包含两个派生QObject类型的注册模块。在QML中,我可以调用MasterController方法。我看到它在调试器里。但是,当执行返回到QML代码时,返回的变量为“未定义”。所以,我看不懂它的任何属性。我为你读了问题和答案。但是,它并没有给我足够的信息来正确理解这一点 MyType.hC++ 为什么我的Q_可调用方法返回的自定义QObject在我的QML中没有定义?,c++,qt,qml,C++,Qt,Qml,我有一个MasterController QObject,它带有一个Q_可调用方法,该方法返回对MyType对象的常量引用,MyType是从QObject派生的另一种类型。我在main()中注册这两个。我实例化了一个MasterController,并将其添加到main中的根上下文中。在我的QML中,我导入包含两个派生QObject类型的注册模块。在QML中,我可以调用MasterController方法。我看到它在调试器里。但是,当执行返回到QML代码时,返回的变量为“未定义”。所以,我看不懂
#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
主控制器
#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
main.cpp(主要是锅炉板)
QoObject不可复制,因此您无法传递QoObject的引用,而是传递指针:
Q_INVOKABLE QObject* getData();
QObject*MasterController::getData()
{
返回&_myData;
}
只是好奇,如果您将可调用函数更改为返回指针而不是常量ref,它是否有效?如果我按照您的建议将返回类型更改为(非常量)MyType指针,那么main.qml中的“var data=masterController.getData();”调用将完全失败。Qt Creator调试器中的应用程序输出窗口显示“错误:未知方法返回类型:models::MyType*”。将getData返回类型更改为QObject*确实有效。我可以发誓我看到过使用自定义类型作为返回类型的示例。但是,我想使用QObject*并不会损失任何东西。谢谢
#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
#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();
}
}
}
Q_INVOKABLE QObject* getData();