Qt Q_GADGET未知方法返回类型
我有一个班级在我的窗口。这堂课叫什么 我的窗户Qt Q_GADGET未知方法返回类型,qt,qml,qgadget,Qt,Qml,Qgadget,我有一个班级在我的窗口。这堂课叫什么 我的窗户 class MyWindow : public QObject { Q_OBJECT Q_PROPERTY(int nbMatch READ GetNbMatch NOTIFY matchChangedQMLL) public: explicit MyWindow(QObject *parent = nullptr); explicit MyWindow(AsyncCalendarGetter& calendar,
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;
};
有人有主意吗
谢谢您可以在这里找到有关
Q\u DECLARE\u元类型的信息
:
根据它,您应该执行以下步骤来解决问题:
Date
qRegisterMetaType()
在引擎加载(url)之前的某处代码>
main()
中有qqqmlapplicationengine;
来加载和运行QML)
更新:同样
std::string
不受QML直接支持,您应该使用QString
您是否尝试将Q\u DECLARE\u元类型(Date)
添加到Date.h?它似乎工作得更好,但我已经有一条错误消息:错误:未知方法返回类型:std::stringChange std::string到QString
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
}
}