如何正确子类化并插入视图QAbstractProxyModel

时间:2011-11-30 10:26:21

标签: c++ qt

为了正确地对QAbstractProxyView进行子类化并使其与QListView等视图一起工作,我该怎么办?到目前为止,我已经这样做了:

class Proxy : public QAbstractProxyModel
{
    Q_OBJECT
public:
    explicit Proxy(QAbstractItemModel* source_model = 0, QObject *parent = 0);
    QModelIndex mapToSource(const QModelIndex & proxyIndex) const;
    QModelIndex mapFromSource(const QModelIndex & sourceIndex) const;
    virtual QModelIndex index(int row, int column,
                              const QModelIndex &parent = QModelIndex()) const;
     virtual QModelIndex parent(const QModelIndex &child) const;
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex&, int = Qt::DisplayRole) const;

signals:

public slots:

};

此类中的每个函数都在单独的cpp文件中实现。不幸的是在我的Ui课程中做了:

Proxy* p = new Proxy;
p->setSourceModel(model_);//model is of class Model
listView->setModel(p);

不起作用;模型中的数据不会显示在listView

1 个答案:

答案 0 :(得分:2)

如果它适用于QListView,则应该使模型继承自QAbstractListModel。您只需要实现三个方法:构造函数rowCountdata。有关详情,请参阅此帖子:How to create a custom model for a QListView in Qt