Quantcast
Channel: Active questions tagged qtquick2 - Stack Overflow
Viewing all articles
Browse latest Browse all 137

Qt 5.12.9 Q_GADGET class not working properly

$
0
0

I have this classes:

description_value_model.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

Than I have a qml proxy that use SupportModel:

#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

In main.cpp I create the proxy and fill the support like that:

#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();}

And in qml I try to show the info of the support:

import QtQuick 2.12import QtQuick.Window 2.12import QtQuick.Controls 2.12Window {    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            }        }    }}

This code is running fine on Qt 5.15.2 but it only shows the title "Contactless card" on Qt 5.12.9. Am I doing something wrong?


Viewing all articles
Browse latest Browse all 137

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>