QT QML - 从另一个类访问 qml 模型
QT QML - access qml model from another class
我需要有"myclient"类来处理来自套接字的输入数据。数据到达后,它应该从qml模型类"mymodel"调用函数"add"。如何访问在 main.cpp 中创建的模型的实例?
下面是一个具有文本输入功能的示例:
类:主要.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "mymodel.h"
#include "myclient.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
MyModel *theModel=new MyModel;
engine.rootContext()->setContextProperty("theModel", theModel);
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.load(url);
myclient cli; //doesn't add the data to list in it's constructor
theModel.add("Added in main.cpp"); //works as expected
return app.exec();
}
现在这里是类 myModel.h
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QObject>
#include <QAbstractTableModel>
#include <QModelIndex>
#include <QHash>
#include <QVariant>
#include <QByteArray>
#include <QList>
#include <QDebug>
class MyModel: public QAbstractListModel
{
Q_OBJECT
public:
MyModel(QObject* parent=nullptr): QAbstractListModel (parent)
{
innerModel.append("Bob");
innerModel.append("Patrick");
innerModel.append("Alice");
}
Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
if (parent.isValid())
return 0;
return innerModel.size();
}
Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const override
{
if (parent.isValid())
return 0;
return 2;
}
Q_INVOKABLE QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
{
qDebug() << index << role;
switch (role)
{
case Qt::DisplayRole:
case Qt::UserRole + 1:
return innerModel.at(index.row());
case Qt::UserRole + 2:
return QString::number(index.row() + 1);
}
return QVariant();
}
Q_INVOKABLE QHash<int,QByteArray> roleNames() const override
{
QHash<int,QByteArray> roles;
roles.insert(Qt::UserRole + 1, "name");
roles.insert(Qt::UserRole + 2, "index");
return roles;
}
Q_INVOKABLE void add(QString const& name)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
innerModel.append(name);
endInsertRows();
}
private:
QList<QString> innerModel;
};
#endif // MYMODEL_H
最后是我的客户。
我尝试过这种方式,但我知道我正在这里创建 myModel 的另一个实例
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include "mymodel.h"
class myclient
{
public:
myclient()
{
MyModel mod;
mod.add("Added from myclient");
}
};
#endif // MYCLIENT_H
如何在应用程序的其他部分访问 myModel 的方法(在 main.cpp 中加载到引擎中的实例(?还是有另一种方法可以做到这一点?谢谢
如果要
访问客户端类中的模型,请将其作为构造函数的参数传递,使其成为类的成员。
我的客户.h
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include "mymodel.h"
class myclient
{
public:
myclient(MyModel *model): m_model(model)
{
m_model->add("Added from myclient");
}
private:
MyModel *m_model;
};
#endif // MYCLIENT_H
主.cpp
// ...
myclient cli(&theModel);
// ...
相关文章:
- 最佳做法是从另一个线程访问 qml 中的Q_PROPERTY
- 尝试在 qml 中访问结构成员的 QList
- 通过 id 访问 QML 插槽中的嵌套对象(从 c++ 调用)
- 使用 WebGL 使用 qml 访问客户端上的文件
- 如何从C++访问QML Surface3DSeries?
- 如何在 QML 中访问基于 QObjectList 的模型中的某些元素
- QT QML - 从另一个类访问 qml 模型
- 使用C 访问QT / QML对象
- 如何从qmlregistersingletontype的回调中访问QML加载项目
- QML实例化C 对象.我如何访问他们的方法
- 从C++访问现有的 QML 组件
- 如何从 QML 访问 QModelIndex 行
- 如何从 QT 中的.cpp访问 QML 中的一个复选框
- 检查/访问 QML 中动态创建的对象
- 使用cpp代码从加载的qml访问qml对象
- 从C++访问 QML 对象
- 访问QML中的QList对象
- 从C++访问 QML 的布局镜像附加属性
- 访问QML中使用setProperty设置的属性
- 从 QT 中的C++代码访问 QML 元素