我有一个MyWindow类。这个课堂电话
MyWindow.h
<template>
<div id="my-map-view"></div>
</template>
export default {
methods: {
async createMapView() {
const mapInstance = await HMSMap.create("my-map-view", {
zoomGesturesEnabled: true,
zoomControlsEnabled: true,
cameraPosition: {
target: { lat: 39.707187, lng: 37.530353 },
zoom: 7
}
});
}
},
mounted() {
document.addEventListener("deviceready", async () => {
await HMSMap.init();
this.createMapView();
});
}
};
Date.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();
}
// ...
}
当我在QML中调用第一个函数getFirstMatch时,它起作用了。 但是,第二个功能gtFirstDate不起作用,我收到一条错误消息:
qrc:/main.qml:27:错误:未知方法返回类型:日期
我的QML
#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;
};
有人有主意吗?
谢谢
答案 0 :(得分:1)
您可以在此处找到有关Q_DECLARE_METATYPE
的信息:
https://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE
根据它,您应该执行以下步骤来解决您的问题:
Q_DECLARE_METATYPE(Date)
后添加Date
qRegisterMetaType<Date>();
之前的某个地方添加engine.load(url);
(我假设您在QQmlApplicationEngine engine;
中有main()
个要加载和运行QML的内容
更新:此外,QML不直接支持std::string
,您应该使用QString