Q_GADGET 未知方法 return 类型
Q_GADGET Unknown method return type
我有一个 class MyWindow。这个class调用
MyWindow.h
class MyWindow : public QObject
{
Q_OBJECT
Q_PROPERTY(int nbMatch READ GetNbMatch NOTIFY matchChangedQMLL)
public:
explicit MyWindow(QObject *parent = nullptr);
explicit MyWindow(AsyncCalendarGetter& calendar, QObject *parent = nullptr);
~MyWindow();
Q_INVOKABLE QString getFirstMatch() {
return QString::fromUtf8(calendar->GetCalendar().front().GetDate().toString().c_str());
}
Q_INVOKABLE Date getFirstDate() {
return calendar->GetCalendar().front().GetDate();
}
// ...
}
Date.h
#pragma once
#include <string>
#include <sstream>
#include <QObject>
#include <iostream>
class Date
{
Q_GADGET
Q_PROPERTY(std::string dateStr READ toString)
public:
Date(std::string&& str);
Date();
Date(int day, int month, int year, int h, int m);
friend std::ostream& operator<<(std::ostream& os, const Date& obj);
Q_INVOKABLE std::string toString() const {
std::stringstream ss;
ss << *this;
return ss.str();
}
private:
int day = 1;
int month = 1;
int year = 1970;
int h = 0;
int m = 0;
};
当我在我的 QML 中调用第一个函数 getFirstMatch 时,它起作用了。
但是,第二个函数 gtFirstDate 不起作用,我有一条错误消息:
qrc:/main.qml:27: Error: Unknown method return type: Date
我的QML
Connections {
target: mainmywindow
onMatchChangedQMLL: {
lbl0.text = "" + Number(mainmywindow.nbMatch) + " -> " + qsTr(mainmywindow.getFirstMatch()) // WORKS
lbl1.text = "" + Number(mainmywindow.nbMatch) + " -> " + qsTr(mainmywindow.getFirstDate().toString()) // DOES NOT WORK
}
}
有人有想法吗?
谢谢
您可以在此处找到有关 Q_DECLARE_METATYPE
的信息:
https://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE
根据它,您应该执行以下步骤来解决您的问题:
- 在声明
Date
后添加Q_DECLARE_METATYPE(Date)
- 在
engine.load(url);
之前的某处添加qRegisterMetaType<Date>();
(我假设您在 main()
中有 QQmlApplicationEngine engine;
要加载和 运行 QML)
更新: 另外 std::string
QML 不直接支持,你应该使用 QString
我有一个 class MyWindow。这个class调用
MyWindow.h
class MyWindow : public QObject
{
Q_OBJECT
Q_PROPERTY(int nbMatch READ GetNbMatch NOTIFY matchChangedQMLL)
public:
explicit MyWindow(QObject *parent = nullptr);
explicit MyWindow(AsyncCalendarGetter& calendar, QObject *parent = nullptr);
~MyWindow();
Q_INVOKABLE QString getFirstMatch() {
return QString::fromUtf8(calendar->GetCalendar().front().GetDate().toString().c_str());
}
Q_INVOKABLE Date getFirstDate() {
return calendar->GetCalendar().front().GetDate();
}
// ...
}
Date.h
#pragma once
#include <string>
#include <sstream>
#include <QObject>
#include <iostream>
class Date
{
Q_GADGET
Q_PROPERTY(std::string dateStr READ toString)
public:
Date(std::string&& str);
Date();
Date(int day, int month, int year, int h, int m);
friend std::ostream& operator<<(std::ostream& os, const Date& obj);
Q_INVOKABLE std::string toString() const {
std::stringstream ss;
ss << *this;
return ss.str();
}
private:
int day = 1;
int month = 1;
int year = 1970;
int h = 0;
int m = 0;
};
当我在我的 QML 中调用第一个函数 getFirstMatch 时,它起作用了。 但是,第二个函数 gtFirstDate 不起作用,我有一条错误消息:
qrc:/main.qml:27: Error: Unknown method return type: Date
我的QML
Connections {
target: mainmywindow
onMatchChangedQMLL: {
lbl0.text = "" + Number(mainmywindow.nbMatch) + " -> " + qsTr(mainmywindow.getFirstMatch()) // WORKS
lbl1.text = "" + Number(mainmywindow.nbMatch) + " -> " + qsTr(mainmywindow.getFirstDate().toString()) // DOES NOT WORK
}
}
有人有想法吗?
谢谢
您可以在此处找到有关 Q_DECLARE_METATYPE
的信息:
https://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE
根据它,您应该执行以下步骤来解决您的问题:
- 在声明
Date
后添加 - 在
engine.load(url);
之前的某处添加
Q_DECLARE_METATYPE(Date)
qRegisterMetaType<Date>();
(我假设您在 main()
中有 QQmlApplicationEngine engine;
要加载和 运行 QML)
更新: 另外 std::string
QML 不直接支持,你应该使用 QString