从QT C ++返回自定义对象并在QML中打印属性

时间:2017-09-25 08:57:44

标签: c++ qt qml bind

我需要从C ++传递给QML一个对象来打印它。 所以,我有这个.h:

    #ifndef SETTINGSOVEN_H
#define SETTINGSOVEN_H

#include <QObject>
#include <objects/settings.h>

class settingsoven : public QObject
{
    Q_OBJECT
    public:
    explicit settingsoven(QObject *parent = nullptr);

    Q_INVOKABLE Settings loadStartPage();


    Q_PROPERTY(Settings settingsObject READ getSettings WRITE setSettings NOTIFY settingsChanged)

    signals:
    void settingsChanged();

    public slots:
    Settings getSettings();
    void setSettings(Settings settingsObject);

    private:
    Settings m_settingsObject;
};

#endif // SETTINGSOVEN_H

和我的cpp:

#include "settingsoven.h"
#include "objects/singleton.h"
#include "functions/jsonfunctions.h"

settingsoven::settingsoven(QObject *parent) : QObject(parent)
{

}

Settings settingsoven::loadStartPage()
{
    singleton::instance().settingsList = JsonFunctions::getConfiguration(singleton::instance().ovenClicked.id, singleton::instance().token_type, singleton::instance().access_token);
    m_settingsObject = singleton::instance().settingsList.at(0);
    return m_settingsObject;
}

Settings settingsoven::getSettings()
{
    return m_settingsObject;
}

void settingsoven::setSettings(Settings settingsObject)
{
    m_settingsObject = settingsObject;
    emit settingsChanged();
}

并按照我的qml代码:

...

Page {
    width: Screen.width
    height: Screen.height

    Component.onCompleted: {
        settingsOvenToQML.loadStartPage();
        //console.log("test:" + configuration.gmt);

        console.log("proopertie print:" + settingsOvenToQML.settingsObject.id);
        //i need to print here the properties
    }

...

我需要在qml中打印的自定义对象: 设置

#ifndef SETTINGS_H
#define SETTINGS_H

#include <QString>
#include <QVariant>

class Settings
{
    public:
    Settings();

    QString id;
    QString gmt;
    QVariant rtc;
    QString thing_name;
    QString sw_gw;
    QVariant sw_ui;
    QString wlan_ip_address;
    QString eth_ip_address;
    QString hostname;
    qint32 mdl;
    quint32 mdl_code;
    quint32 tmp_unit;
    quint32 h2o_hdns;
    quint32 oven_no;
    quint32 alarm_wash_to;
    qint32 clean_to;
    qint8 clean_notice_to;
    qint8 decal_notice_to;
    bool gas_test;
    bool warn_decal_on;
    bool warn_prb_on;
    qint8 primary_prb;
    qint8 time_fmt;
    qint8 date_fmt;
    bool preheat_otp;
    bool cooldown_opt;
    qint8 ext_prb_type;
    qint8 haccp_scan;
    bool h2o_drain;
    bool chiller_opt;
    QString lang;
    qint8 backlight_lvl;
    QString video_ver;
    QString oven_name;
    QString video_build;
    QString os_ver;
    QString os_build;
    QString q1_ver;
    QString q1_build;
    QString q1_check;
    QString q2_ver;
    QString q2_build;
    QString q2_check;
    QString chill_ver;
    QString chill_build;
    QString chill_check;
    QString mac_addr;
};

#endif // SETTINGS_H

我是新的qt程序员,我知道如何从qt打印对象。 我相信我的代码是对的,但不行。 哪里错了?它可以直接在c ++的qml中打印自定义对象吗? 一些帮助 ?感谢

1 个答案:

答案 0 :(得分:2)

我不确定你的意思是&#34; print&#34;,但是如果你想要能够访问一个对象的属性,它必须是QObject派生的,或者具有Q_GADGET宏,因此MOC可以生成QML所需的元数据,以便知道对象是什么。

此外,您必须以某种方式公开属性,公共成员将不适用于QML。您需要Q_PROPERTY或通过Q_INVOKABLE getter返回值。

#define QPROP(type, name) \
  Q_PROPERTY(type name MEMBER m_##name) \
  type m_##name;

class Settings {
    Q_GADGET
    QPROP(QString, id)
    QPROP(QString, gmt)
    QPROP(QVariant, rtc)
    // and so on
public:
    Settings();
}

Q_DECLARE_METATYPE(Settings) // last but not least