为了正确地对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
。
答案 0 :(得分:2)
如果它适用于QListView,则应该使模型继承自QAbstractListModel
。您只需要实现三个方法:构造函数rowCount
和data
。有关详情,请参阅此帖子:How to create a custom model for a QListView in Qt