为什么我无法从 QML 读取公开的C++数据?

Why can't I read exposed C++ datas from QML?

本文关键字:C++ 数据 读取 QML 为什么      更新时间:2023-10-16

我创建了这个类,将其用作数据容器。我从json中读取了一些数据(在c++中),并填充了一个GUIArea列表,该列表存储在dataHandler的m_guiAreas列表中。在QML的某个时刻,我向数据处理程序请求一系列selectedAreas。数据处理程序填充QList m_selectedGuiAres并发出selectedAreasChanged()信号。现在,我希望使用一个矩形网格来填充选定的数据,但我什么也没看到。在C++级别,当selectedAreasChanged()发出时,m_selectedGuiAres结果填充了正确的数据,但在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指南中看到了这一点)。该方法初始化datas读取存储在m_GUIAreas中的数据,然后选择一个发送到m_selectedGUIAs中的QML,最后发出信号selectedGUAsChanged()。

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是文本中的矩形,也是属性文本的别名。

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"

main.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
}
}
}
}