QtQuick - qml:28:错误:未知方法返回类型:自定义类型
QtQuick - qml:28: Error: Unknown method return type: custom type
我想做什么
我有两个班级ObjectA
和ObjectB
。ObjectA
是ObjectB
的组成。 我想创建一个按钮,用于调用 QML 中ObjectB
insert(int num, ObjectA &A)
函数。
我的代码:
对象a.h
//...
#include <QObject>
#include <QDateTime>
class ObjectA
{
public:
explicit ObjectA(QString str);
//...
private:
//...
};
//...
objectb.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:
//...
};
//...
对象.cpp
include "objecta.h"
ObjectA::ObjectA(QString str)
{
//...
}
对象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){
//...
}
//...
主.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();
}
主.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
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))
}
}
我得到的错误
当我单击按钮时,我收到此错误消息:
qrc:/Page.qml:28: Error: Unknown method return type: ObjectA
我认为问题出在objectb.generate(strfield.text)
由于QML无法识别自定义类型而无法返回ObjectA
。我该如何解决?
编辑:添加了ObjectA
和ObjectB
的头文件,以及ObjectA
的cpp文件
临时解决方案:
避免在 QML 中使用自定义返回类型。
objectb.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:
//...
};
//...
对象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){
//...
}
//...
请注意Button
中的onClicked
。
Page.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)
}
}
相关文章:
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 具有引用返回类型的重写方法上的协变返回类型无效
- Java 调用 dll 字符串返回类型方法
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 如何拥有两个名称相同但返回类型不同的纯虚拟方法
- qml 未知方法返回类型:ArchiveFile*,即使调用了 qmlRegisterUncreatableType
- std::d eclval vs crtp,无法从不完整类型推断方法返回类型
- 将返回类型推断为模板参数类型方法
- dlsym() 解决方法返回类型
- 我如何根据某些模板参数影响模板类方法的返回类型
- 访问方法的返回类型
- 更改基类方法在其派生类中的返回类型
- 具有协变返回类型的方法在 VC++ 上崩溃
- 如何检查模板类方法返回类型
- 如何使用类型专用化模板方法,该类型本身就是一个模板,其中只有返回类型依赖于模板类型
- 解释C 中三种返回类型的方法
- 覆盖方法返回类型,在C 中使用不完整的派生类
- 存储方法的返回类型和参数类型
- C 多重继承,虚拟方法覆盖问题和协变量返回类型