Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt Q_GADGET未知方法返回类型_Qt_Qml_Qgadget - Fatal编程技术网

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
        }
     }