簡體   English   中英

QtQuick - qml:28: 錯誤:未知方法返回類型:自定義類型

[英]QtQuick - qml:28: Error: Unknown method return type: custom type

我想做什么

我有兩個類ObjectAObjectB ObjectA是的組合物ObjectB 我想打一個按鈕,調用函數insert(int num, ObjectA &A)ObjectB在QML。

我的代碼:

對象a.h

//...

#include <QObject>
#include <QDateTime>

class ObjectA
{
public:
    explicit ObjectA(QString str);
    //...    

private:
    //...
};

//...

對象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:
    //...

};

//...

對象.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){ 

    //...
}

//...

主程序

#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();
}

主文件

import QtQuick 2.12
import QtQuick.Window 2.12

import com.mycompany.ObjectB 1.0

Window {
    //...

    ObjectB {
        id: objectb
    }

    Page {} //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))
    }

}




我得到的錯誤

當我單擊按鈕時,我收到此錯誤消息:

qrc:/Page.qml:28: Error: Unknown method return type: ObjectA

我認為這個問題是objectb.generate(strfield.text)未能返回ObjectA ,因為QML不能識別自定義類型。 我該如何解決?

編輯:新增的頭文件ObjectAObjectB ,以及CPP文件ObjectA

臨時解決方案:

避免在 QML 中使用自定義返回類型。

對象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:
    //...

};

//...

對象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){ 

    //...
}

//...

注意ButtononClicked

頁面.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM