我有一个班级 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();
}
// ...
}
日期.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:错误:未知方法返回类型:日期
我的 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
}
}
有人有想法吗?
谢谢