QtQuick-qml:28:错误:未知方法返回类型:自定义类型

时间:2020-02-06 10:27:03

标签: c++ qt qml

我要做什么

我有两个班ObjectAObjectBObjectAObjectB的组成。 我想创建一个按钮,以从QML中的insert(int num, ObjectA &A)调用函数ObjectB

我的代码:

objecta.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:
    //...

};

//...

objecta.cpp

include "objecta.h"

ObjectA::ObjectA(QString str)
{
    //...
}

objectb.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

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)上,无法返回ObjectA,因为QML无法识别自定义类型。我该怎么解决?

编辑:添加了ObjectAObjectB的头文件以及ObjectA的cpp文件

1 个答案:

答案 0 :(得分:0)

临时解决方案:

避免在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:
    //...

};

//...

objectb.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){ 

    //...
}

//...

请注意onClicked中的Button

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)
    }

}