Qt在ItemDelegate外部访问模型数据 [英] Qt accessing model data outside ItemDelegate

查看:243
本文介绍了Qt在ItemDelegate外部访问模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些继承QAbstractListModel的模型类:

I have some model class that inherits QAbstractListModel:

VehiclesModel.h:

class VehiclesModel : public QAbstractListModel {
    Q_OBJECT

    public:
        enum Roles {
            ImagePathRole = Qt::UserRole + 1,   // QString
            NameRole                            // QString
        };

        virtual int rowCount(const QModelIndex & parent = QModelIndex()) const override { ... }
        virtual QVariant data(const QModelIndex & index, int role) const override { ... }
        virtual QHash<int, QByteArray> roleNames() const override {
            QHash<int, QByteArray> roles = QAbstractListModel::roleNames();

            roles[ImagePathRole] = "imagePath";
            roles[NameRole] = "name";

            return roles;
        }
};

main.cpp:

#include "VehiclesModel.h"

int main(int argc, char * argv[]) {
    QGuiApplication app(argc, argv);
    VehiclesModel vehiclesModel;
    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty("vehiclesModel", &vehiclesModel);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

ComboBox显示此模型: main.qml:

ComboBox {
    id: control
    model: vehiclesModel
    delegate: ItemDelegate {
        contentItem: RowLayout {
            Image {
                source: imagePath
            }
            Label {
                text: name
            }
        }
        highlighted: control.highlightedIndex == index
    }
    contentItem: RowLayout {
        Image {
            source: ??imagePath??
        }
        Label {
            text: ??name??
        }
    }
}

我想自定义ComboBox以显示车辆图像和名称.我可以从ItemDelegate访问模型数据,但是如何在ItemDelegate之外访问模型数据?例如,我想访问当前索引数据(ImagePathRoleNameRole)以在contentItem中显示车辆图像和名称.

I want to customize the ComboBox to show vehicle image and name. I can access to model data from ItemDelegate but how to access to model data outside the ItemDelegate? For example I want to access current index data (ImagePathRole and NameRole) to display vehicle image and name in contentItem.

是否可以在不直接调用QAbstractListModel方法(即index()data()方法)并将其设置为Q_INVOKABLE的情况下做到这一点?

Is it possible to do it without calling QAbstractListModel methods directly (i.e. index() and data() methods) and making them Q_INVOKABLE?

推荐答案

目前还没有一种像样的内置方式,不幸的是,我发现这已经是相当长一段时间了,并且我已经考虑过在QML模型功能中为此实现一些功能,但是我还没有时间这样做.

Not in any sort of a decent built-in way at the present time, unfortunately, this is something I've found to be lacking for quite a while, and I've considered implementing something for this in the QML models functionality, but I haven't yet had the time to do so.

目前,您可以自己进行操作(例如您正在讨论),但要以类型安全为代价,等等,或者(我以前通常解决此问题的方式),您可以创建一个QObject子类代表模型中的单个项目(ItemDataThing或您选择调用的任何项目);为它提供源模型索引,属性,并使其代表模型中数据的单个实例.

For the time being, you can either do it yourself (like you're discussing), at the cost of type-safety and so on, or (the way I've typically tackled this before), you can create a QObject subclass to represent a single item in the model (ItemDataThing or whatever you choose to call it); provide it with a source model & index, properties, and let it represent a single instance of data from the model.

类似的东西:

class ImageDataThing : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString imagePath READ imagePath NOTIFY imagePathChanged)
    Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)
    Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged)

public:
    QString imagePath() const;
    QAbstractItemModel *model() const;
    void setModel(const QAbstractItemModel *newModel);
    int index() const;
    void setIndex(int newIndex);
signals:
    void imagePathChanged(const QString &imagePath);
    void modelChanged(QAbstractItemModel *model);
    void indexChanged(int indexChanged);
};

...,并且在您的实现中,只要设置了模型,就钩住更改信号(例如,rowsInserted,rowsRemoved等)以更改存储的索引(如果提供),以将其映射到正确的位置.模型.

... and in your implementation, whenever the model is set, hook the change signals (e.g. rowsInserted, rowsRemoved, ...) to alter the stored index (if provided) to keep it mapped to the correct place in the model.

在模型数据获取器(例如,此处为imagePath)中,访问模型实例(使用索引)以获取数据并返回.

In the model data getters (here, imagePath for instance), access the model instance (using the index) to grab the data out, and return it.

这显然有很多重复的缺点,但是,另一方面,如果您熟悉模型,类型安全并且可以相当容易地自动生成它,那么它就很容易编写代码.

This has the obvious disadvantage of being a lot of boilerplate, but on the other hand, it's easy-to-write code if you are familiar with models, type-safe, and one could autogenerate it fairly easily.

这篇关于Qt在ItemDelegate外部访问模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆