gpt4 book ai didi

c++ - 为什么我不能从 QML 读取暴露的 C++ 数据?

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:47 25 4
gpt4 key购买 nike

我制作这个类是为了将它用作数据容器。我从 json 中读取了一些数据(在 C++ 中)并填充了我存储在 dataHandler 的 m_guiAreas 列表中的 GUIArea 列表。在 QML 的某个时刻,我向 dataHandler 请求了一系列 selectedAreas。 DataHandler 填充 QList m_selectedGuiAreas 并发出 selectedAreasChanged() 信号。现在我希望看到一个矩形网格,其中填充了选定的数据,但我什么也没看到。在 C++ 级别,当发出 selectedAreasChanged() 时,m_selectedGuiAreas 结果填充了正确的数据,但在 QML 级别,它似乎是空的,或者数据可能没有正确读取。

这是我用作包装器将数据带到 QML 级别的类:

class GUIArea : public QObject
{
Q_OBJECT
Q_PROPERTY(QString id READ id )
Q_PROPERTY(QString configurations READ configurations )

...

public:
explicit GUIArea(QObject *parent = nullptr): QObject (parent) {}

QString id() {return m_id;}
void id(QString newId) {m_id = newId;}

QString configurations() {return m_configuration; }
void configurations(QString newConfiguration) {m_configuration = newConfiguration;}

...

private:
QString m_id;
QString m_configuration;
};

下面是 dataHandler 类,我在其中声明从 Json 读取的数据列表,并将其从 Qlist 转换为 QQmlPropertyList(我在一些 QML 指南中看到了将 c++ 属性公开给 QML)。initializeDatas 方法读取存储在 m_GUIAreas 中的数据,然后在 m_selectedGUIAreas 中选择要发送到 QML 的数据,最后发出信号 selectedGUIAsChanged()。

class dataHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<GUIArea> selectedGuiAreas READ selectedGuiAreas NOTIFY selectedAreasChanged)

public:
explicit dataHandler(QObject *parent = nullptr);
static dataHandler* instance();

QQmlListProperty<GUIArea> selectedGuiAreas();
...

public slots:
void initializeDatas(const json& blocksList);
...

signals:
...
void selectedAreasChanged();
...
private:
...
QList<GUIArea *> m_guiAreas;
QList<GUIArea *> m_selectedGuiAreas;
};

在主文件中,dataHandler 被声明为一个属性:这是代码:

  QQuickView view;
...
view.engine()->rootContext()->setContextProperty("dataHandler", dataHandler::instance());
...
view.show();

下面是我想在 QML 中可视化的页面的一部分。AreaButton 是 Text 中的一个 Rectangle 和属性文本的别名。

Grid {
id: areasButtonsGrid
columns: 4
anchors.fill: parent

Repeater {
model: dataHandler.selectedGuiAreas
delegate:
AreaButton {
text: qsTr(model.modelData.programName)
}
}
}

最佳答案

代码不完整,无法分析,所以我只提供一个工作代码,以便您分析问题所在:

main.cpp

#include <QGuiApplication>
#include <QQmlContext>
#include <QQuickView>
#include <QTimer>

class GUIArea : public QObject
{
Q_OBJECT
Q_PROPERTY(QString id READ id CONSTANT)
Q_PROPERTY(QString configurations READ configurations CONSTANT)
public:
GUIArea(const QString & id="", const QString & configurations="", QObject *parent=nullptr):
QObject(parent), m_id(id), m_configurations(configurations)
{}
QString id() const{return m_id;}
QString configurations() const{return m_configurations;}
void setId(const QString &id){
m_id = id;
}
void setConfigurations(const QString &configurations){
m_configurations = configurations;
}
private:
QString m_id;
QString m_configurations;
};

class DataHandler: public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<GUIArea> selectedGuiAreas READ selectedGuiAreas NOTIFY selectedAreasChanged)
using QObject::QObject;
public:
static DataHandler& instance(){
static DataHandler handler;
return handler;
}
QQmlListProperty<GUIArea> selectedGuiAreas(){
return QQmlListProperty<GUIArea>(this, m_selectedGuiAreas);
}
void append(GUIArea *area){
if(area){
m_selectedGuiAreas << area;
emit selectedAreasChanged();
}
}
signals:
void selectedAreasChanged();
private:
QList<GUIArea *> m_selectedGuiAreas;
};

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<GUIArea>();
QQuickView view;
view.rootContext()->setContextProperty("dataHandler", &DataHandler::instance());
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();
QTimer timer;
static int counter =0;
QObject::connect(&timer, &QTimer::timeout, [](){
GUIArea *area = new GUIArea(QString::number(counter),
QString("configuratio-%1").arg(counter),
&DataHandler::instance());
DataHandler::instance().append(area);
counter++;
});
timer.start(1000);
return app.exec();
}
#include "main.moc"

ma​​in.qml

import QtQuick 2.12

Grid {
id: areasButtonsGrid
columns: 5
width: 640
height: 480
spacing: 20
Repeater {
model: dataHandler.selectedGuiAreas
delegate:
Rectangle{
width: 100
height: 100
color: "blue"
Text {
anchors.fill: parent
text: qsTr(model.modelData.configurations)
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
}
}

关于c++ - 为什么我不能从 QML 读取暴露的 C++ 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54306645/

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