首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让一个自定义的C++类成为一个QML类型?

如何让一个自定义的C++类成为一个QML类型?
EN

Stack Overflow用户
提问于 2019-03-05 04:02:51
回答 1查看 972关注 0票数 0

我正在尝试将一个简单的结构从C++推送到QML,但我似乎无法注册它:

messages.h

#pragma once

#include <QString>
#include <QMetaType>

struct Ticket {
  Q_GADGET
  Q_PROPERTY(QString name MEMBER name)
  Q_PROPERTY(QString description MEMBER description)

  public:
  QString name;
  QString description;
};
Q_DECLARE_METATYPE(Ticket);

我正在将其提供给QML:

main.cpp

#include <QtWidgets>

#include "MyWindow.h"
#include "messages.h"

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  qmlRegisterType<Ticket>("myself", 1, 0, "Ticket");
  MyWindow the_window;
  the_window.show();

  return app.exec();
}

编辑:包含QML以供参考

import QtQuick.Layouts 1.11
import QtQuick 2.11

RowLayout {
  id: layout
  property var tickets

  onTicketsChanged: {
    console.log("dum dum dum");
  }
}

编译时,我得到以下错误:

/Users/myself/lib/Qt/5.11.2/clang_64/lib/QtQml.framework/Headers/qqmlprivate.h:101:24: error: only virtual member
      functions can be marked 'override'
        ~QQmlElement() override {
                       ^
/Users/myself/lib/Qt/5.11.2/clang_64/lib/QtQml.framework/Headers/qqmlprivate.h:107:50: note: in instantiation of
      template class 'QQmlPrivate::QQmlElement<Ticket>' requested here
    void createInto(void *memory) { new (memory) QQmlElement<T>; }
                                                 ^
/Users/myself/lib/Qt/5.11.2/clang_64/lib/QtQml.framework/Headers/qqml.h:292:33: note: in instantiation of function
      template specialization 'QQmlPrivate::createInto<Ticket>' requested here
        sizeof(T), QQmlPrivate::createInto<T>,
                                ^
/Users/myself/dev/my_project/src/MyWindow.cpp:69:3: note: in instantiation of function template specialization
      'qmlRegisterType<Ticket>' requested here
  qmlRegisterType<Ticket>("myself", 1, 0, "Ticket");
  ^
1 error generated.

有没有办法,使我的QML中的票证结构可用,这样我就可以将值从C++推送到QML?或者,有没有办法在qmlRegisterType上下文中找出virtual member override错误的含义?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54990704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档