gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:23:14 25 4
gpt4 key购买 nike

我需要从 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 打印对象。我相信我的代码是正确的,但不起作用。我哪里错了?是否可以直接在 qml 中从 C++ 打印自定义对象?一些帮助 ?谢谢

最佳答案

我不确定你所说的“打印”是什么意思,但是如果你想能够访问一个对象的属性,它必须是 QObject 派生的,或者有 Q_GADGET 宏,因此 MOC 可以生成 QML 需要的必要元数据,以便了解对象是什么。

此外,您必须以某种方式公开属性,公共(public)成员不适用于 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

关于c++ - 从 QT C++ 返回自定义对象并在 QML 中打印属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com