[英]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.