[英]QtQuick - qml:28: Error: Unknown method return type: custom type
I have two classes ObjectA
and ObjectB
.我有两个类
ObjectA
和ObjectB
。 ObjectA
is a composition of ObjectB
. ObjectA
是的组合物ObjectB
。 I want to make a button that calls the function insert(int num, ObjectA &A)
from ObjectB
in QML.我想打一个按钮,调用函数
insert(int num, ObjectA &A)
从ObjectB
在QML。
objecta.h对象a.h
//...
#include <QObject>
#include <QDateTime>
class ObjectA
{
public:
explicit ObjectA(QString str);
//...
private:
//...
};
//...
objectb.h对象b.h
//...
#include <QObject>
#include <QList>
#include <QDebug>
#include "objecta.h"
class ObjectB : public QObject
{
Q_OBJECT
public slots:
ObjectA generate(QString str);
void insert(int priority, ObjectA &A);
public:
ObjectB(QObject *parent = nullptr);
//...
private:
//...
};
//...
objecta.cpp对象.cpp
include "objecta.h"
ObjectA::ObjectA(QString str)
{
//...
}
objectb.cpp对象b.cpp
include "objectb.h"
//Object A is a composition of Object B
ObjectA ObjectB::generate(QString str){
ObjectA *A = new ObjectA(str);
return *A;
}
// ^ I will use the *A as the parameter for the &A in function insert()
void ObjectB::insert(int num, ObjectA &A){
//...
}
//...
main.cpp主程序
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "objectb.h"
int main(int argc, char *argv[])
{
ObjectB B;
qmlRegisterType<ObjectB>("com.mycompany.ObjectB", 1, 0, "ObjectB");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
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.12
import QtQuick.Window 2.12
import com.mycompany.ObjectB 1.0
Window {
//...
ObjectB {
id: objectb
}
Page {} //Page.qml
}
Page.qml页面.qml
import QtQuick 2.0
import QtQuick.Controls 2.1
Row {
id: row
anchors.centerIn: parent
spacing: 20
TextField {
id: strfield
}
ComboBox {
id: numbox
width: 200
model: [ "1", "2", "3" ]
}
Button {
id: btninsert
text: "Insert"
highlighted: true
onClicked: objectb.insert(numbox.currentIndex+1,objectb.generate(strfield.text))
}
}
When I click on the button, I get this error message:当我单击按钮时,我收到此错误消息:
qrc:/Page.qml:28: Error: Unknown method return type: ObjectA
I think the problem is on objectb.generate(strfield.text)
failed to return ObjectA
since QML can't recognize the custom type.我认为这个问题是
objectb.generate(strfield.text)
未能返回ObjectA
,因为QML不能识别自定义类型。 How can I solve it?我该如何解决?
Edit: Added header files of ObjectA
and ObjectB
, and cpp file of ObjectA
编辑:新增的头文件
ObjectA
和ObjectB
,以及CPP文件ObjectA
Temporary solution:临时解决方案:
Avoid using custom return type in QML.避免在 QML 中使用自定义返回类型。
objectb.h对象b.h
//...
#include <QObject>
#include <QList>
#include <QDebug>
#include "objecta.h"
class ObjectB : public QObject
{
Q_OBJECT
public slots:
// A new function that combine both generate and insert function
void btnInsert(int priority, QString str)
ObjectA generate(QString str);
void insert(int priority, ObjectA &A);
public:
ObjectB(QObject *parent = nullptr);
//...
private:
//...
};
//...
objectb.cpp对象b.cpp
include "objectb.h"
//Implement new function
void ObjectB::btnInsert(int priority, QString str)
{
ObjectA *A = new ObjectA(str);
//Code implementation...
}
//Object A is a composition of Object B
ObjectA ObjectB::generate(QString str){
ObjectA *A = new ObjectA(str);
return *A;
}
// ^ I will use the *A as the parameter for the &A in function insert()
void ObjectB::insert(int num, ObjectA &A){
//...
}
//...
Note the onClicked
in Button
.注意
Button
的onClicked
。
Page.qml页面.qml
import QtQuick 2.0
import QtQuick.Controls 2.1
Row {
id: row
anchors.centerIn: parent
spacing: 20
TextField {
id: strfield
}
ComboBox {
id: numbox
width: 200
model: [ "1", "2", "3" ]
}
Button {
id: btninsert
text: "Insert"
highlighted: true
//Modified
onClicked: objectb.insert(numbox.currentIndex+1,strfield.text)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.