在QT中,链接模型无法按预期工作 [英] In QT, chaining models does not work as expected

查看:78
本文介绍了在QT中,链接模型无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个非常基本的QStandardItemModel,里面充满了一些数字.我设法将其显示在QTableView中,没关系.我创建了一个新模型(QAbstractItemModelQAbstractProxyModel的子类),它是现有模型的某种层-需要设置源模型,并且该新层应该对真实模型进行一些转换.

Alright, I have a really basic QStandardItemModel, filled with some numbers. I managed to display it in a QTableView, it's ok. I created a new model ( subclass either of QAbstractItemModel or QAbstractProxyModel ), which is some kind of a layer of an existing model - it is needed to set the sourcemodel, and this new layer should do some transformations on the real one.

我的问题是,在顶层说"c4>成员函数从未调用过的层模型",但是我想通过角色参数更改显示方法.

My problem is, that in the top layer, say "layer model" the data( const QModelIndex & index, int role ) member function never called, however i would like to alter the displaying methods by the role parameter.

这是一个示例代码,该代码演示始终调用原始模型的data(index,role),而从不调用层模型的data(index,role).为什么? QTableView对象如何跳过"顶层的data(index,role)?

Here is a sample code, which demonstrates, that the original model's data(index,role) is always called, whilst the layer model's data(index,role) never. Why? How can the QTableView object "skip" the top layer's data(index,role) ?

#include <QtGui/QApplication>
#include <QtGui>
#include <QStandardItemModel>

class MyModel : public QStandardItemModel
{
public:
    MyModel(const int r, const int c, QObject* parent = 0) : QStandardItemModel(r,c,parent) {}
    QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
        qDebug() << "mymodel data";
        return this->itemFromIndex(index)->data(role);
    }
};

class MyProxyModel : public QAbstractProxyModel
{
public:

    MyProxyModel(QObject* parent = 0) : QAbstractProxyModel(parent) {}
    QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
        return this->sourceModel()->index(row,column,parent);
    }
    QModelIndex parent ( const QModelIndex & index ) const {
        return this->sourceModel()->parent(index);
    }
    QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const
    {
        return sourceIndex;
    }
    QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const
    {
        return proxyIndex;
    }
    QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
        qDebug() << "myproxymodel data";
        return this->sourceModel()->data(index,role);
    }

    int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
        return this->sourceModel()->rowCount(parent);
    }
    int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
        return this->sourceModel()->columnCount(parent);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    MyModel model(8, 2);
    MyProxyModel mymodel;
    mymodel.setSourceModel(&model);

    QTableView tableView;
    tableView.setModel(&mymodel);

    tableView.horizontalHeader()->setStretchLastSection(true);
    for (int row = 0; row < 8; ++row) {
        for (int column = 0; column < 2; ++column) {
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row+1) * (column+1)));
        }

    }
    tableView.show();
    return app.exec();
}

推荐答案

因为QTableView使用模型索引来检索数据,可能是这样的.

Because QTableView uses the model index to retrieve the data, probably something like this.

QModelIndex index = model->index(row, column, parentIndex); 
index.data(Qt::DisplayRole);

您将返回源模型的模型索引,而不是代理模型的索引:

And you are returning the model index of the source model instead of an index to your proxy model:

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->sourceModel()->index(row,column,parent);
}

尝试将模型索引转换为代理模型的索引

Try to convert the model index into an index to your proxy model

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->createIndex(row,column,row);
}

不要忘记将映射重写为源映射和源函数映射.

Don't forget to rewrite the map to source and map from source functions.

解决方案

class MyTableProxyModel : public QAbstractProxyModel
{
    Q_OBJECT
public:
    MyTableProxyModel (QObject* parent = 0) 
        : QAbstractProxyModel(parent) {
    }

    QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const {
        return createIndex(row,column,row);
    }

    QModelIndex parent(const QModelIndex &index) const {
        //Works only for non-tree models
        return QModelIndex();
    }

    QModelIndex mapFromSource(const QModelIndex &source) const {
        return index(source.row(), source.column(), source.parent());
    }

    QModelIndex mapToSource(const QModelIndex &proxy) const {
        return (sourceModel()&&proxy.isValid())
            ? sourceModel()->index(proxy.row(), proxy.column(), proxy.parent()) 
            : QModelIndex();
    }

    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
        qDebug() << "myproxymodel data";
        return mapToSource(index).data(role);
    }

    int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->rowCount(parent) : 0;
    }

    int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->columnCount(parent) : 0;
    }
};

这篇关于在QT中,链接模型无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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