c++ - QT QML - 从另一个类访问 qml 模型

标签 c++ qt qml qt5

我需要有“myclient”类来处理来自套接字的输入数据。数据到达后,它应该从 qml 模型类“mymodel”调用函数“add”。如何访问在 main.cpp 中创建的模型实例?

这是一个具有文本输入功能的示例:

类:main.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

最后是 myclient.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 中加载到引擎中的实例)的方法?或者还有另一种方法吗?谢谢

最佳答案

如果你想在客户端类中访问模型,那么将它作为构造函数的参数传递给类的成员。

myclient.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

main.cpp

// ...
myclient cli(&theModel);
// ...

关于c++ - QT QML - 从另一个类访问 qml 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57153425/

相关文章:

c++ - DOS ASCII Animation Lagging without constant input, Turbo C 编译

C++ std::get<变量> 失败

python - 在 GPU 上提前

c++ - 在 Qt 中清除标志/提示

c++ - Qt 必须在 QWidget 之前构造 QApplication

c++ - 如何使用十六进制值在 C++ 中设置标签颜色?

c++ - 用c++直接写到屏幕

c++ - 限制为子矩形的qt拖放

c++ - QML 和 QQuickImageProvider 大小

c++ - QT - 连接Qml按钮点击Cpp构造函数