Qt 5.12.9 Q_GADGET 类无法正常工作

问题描述 投票:0回答:1

我有这门课:

描述_值_模型.hpp

#ifndef DESCRIPTION_VALUE_MODEL_HPP
#define DESCRIPTION_VALUE_MODEL_HPP

#include <QObject>
#include <QVariant>

class DescriptionValueModel
{
  Q_GADGET
  Q_PROPERTY(QString description READ description)
  Q_PROPERTY(QVariant value READ value)

  public:
    DescriptionValueModel();
    DescriptionValueModel(const QString& description, const QVariant& value);

    const QString& description() const { return m_description; }
    void setDescription(const QString& description) { m_description = description; }

    const QVariant& value() const { return m_value; }
    void setValue(const QVariant& value) { m_value = value; }

  private:
    QString m_description;
    QVariant m_value;
};

Q_DECLARE_METATYPE(DescriptionValueModel)

#endif // DESCRIPTION_VALUE_MODEL_HPP

section_model.hpp

#ifndef SECTION_MODEL_HPP
#define SECTION_MODEL_HPP

#include <QObject>
#include "description_value_model.hpp"

class SectionModel
{
  Q_GADGET
  Q_PROPERTY(QString title READ title)
  Q_PROPERTY(QList<DescriptionValueModel> data READ data)

  public:
    SectionModel();

    const QString& title() const { return m_title; }
    void setTitle(const QString& title) { m_title = title; }

    const QList<DescriptionValueModel>& data() const { return m_data; }
    void addData(const DescriptionValueModel& data) { m_data.append(data); }
    void setData(const QList<DescriptionValueModel>& data) { m_data  = data; }

  private:
    QString m_title;
    QList<DescriptionValueModel> m_data;
};

Q_DECLARE_METATYPE(SectionModel)

#endif // SECTION_MODEL_HPP

support_model.hpp

#ifndef SUPPORT_MODEL_HPP
#define SUPPORT_MODEL_HPP

#include <QObject>
#include "section_model.hpp"

class SupportModel
{
  Q_GADGET
  Q_PROPERTY(SectionModel info READ info)
  Q_PROPERTY(QList<SectionModel> contracts READ contracts)
  Q_PROPERTY(QList<SectionModel> events READ events)

  public:
    SupportModel();

    const SectionModel& info() const { return m_info; }
    void setInfo(const SectionModel& info) { m_info = info; }

    const QList<SectionModel>& contracts() const { return m_contracts; }
    void setContracts(const QList<SectionModel>& contracts) { m_contracts = contracts; }

    const QList<SectionModel>& events() const { return m_events; }
    void setEvents(const QList<SectionModel>& events) { m_events = events; }

  private:
    SectionModel m_info;
    QList<SectionModel> m_contracts;
    QList<SectionModel> m_events;
};

Q_DECLARE_METATYPE(SupportModel)

#endif // SUPPORT_MODEL_HPP

比我有一个使用 SupportModel 的 qml 代理:

#ifndef INFO_PAGE_PROXY_HPP
#define INFO_PAGE_PROXY_HPP

#include <QObject>
#include "support_model.hpp"

class InfoPageProxy : public QObject
{
  Q_OBJECT
  Q_PROPERTY(SupportModel support READ support NOTIFY supportChanged)

  public:
    InfoPageProxy();

    const SupportModel& support() const { return m_support; }
    void setSupport(const SupportModel& support);

  signals:
    void supportChanged();

  private:
    SupportModel m_support;
};

#endif // INFO_PAGE_PROXY_HPP

在 main.cpp 中,我创建代理并填充支持,如下所示:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QDate>
#include "info_page_proxy.hpp"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
  QGuiApplication app(argc, argv);

  InfoPageProxy proxy;
  SectionModel infoModel;
  infoModel.setTitle("Contactless card");
  infoModel.addData({"Serial:", 1234});
  infoModel.addData({"Valid from:", QDate(2020, 1, 1)});
  infoModel.addData({"Valid to:", QDate(2030, 1, 1)});

  SupportModel supportModel;
  supportModel.setInfo(infoModel);

  proxy.setSupport(supportModel);

  QQmlApplicationEngine engine;
  const QUrl url(QStringLiteral("qrc:/main.qml"));
  QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                   &app, [url](QObject *obj, const QUrl &objUrl) {
    if (!obj && url == objUrl)
      QCoreApplication::exit(-1);
  }, Qt::QueuedConnection);

  engine.rootContext()->setContextProperty("infoPageProxy", &proxy);
  engine.load(url);

  return app.exec();
}

在 qml 中我尝试显示支持信息:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Column {
        anchors.fill: parent
        Text {
            height: parent.height * 0.1
            width: parent.width
            text: infoPageProxy.support.info.title
        }

        Repeater {
            model: infoPageProxy.support.info.data
            delegate: Text {
                height: window.height * 0.1
                width: window.width
                text: modelData.description + " " + modelData.value
                elide: Text.ElideLeft
            }
        }
    }
}

此代码在 Qt 5.15.2 上运行良好,但在 Qt 5.12.9 上仅显示标题“非接触式卡”。我是不是做错了什么?

qt qml qtquick2 qt-quick
1个回答
0
投票

我已经弄清楚了。
由于某种原因,在 Qt 5.12 上您必须将属性声明为

Q_PROPERTY(QVariantList contracts READ contracts)

而不是

Q_PROPERTY(QList<SectionModel> contracts READ contracts)

contracts()
函数中,您必须将 QList 转换为 QVariantList。

© www.soinside.com 2019 - 2024. All rights reserved.