[Qt-interest] Problem with QAbstractProxyModel

Jesús Fernández jsfdez at gmail.com
Mon Jan 19 12:56:21 CET 2009


Hi, I'm trying to implement a proxy model for add an icon to the
Qt::DecorationRole for a column.

This is the code:

Header:

> #ifndef IMAGELISTPROXYMODEL_H
>
> #define IMAGELISTPROXYMODEL_H
>
> #include <QAbstractProxyModel>
>
> class ImageListProxyModel : public QAbstractProxyModel
>
> {
>
> Q_OBJECT
>
> Q_ENUMS(Column)
>
> public:
>
> enum Column {
>
> IdColumn=0,
>
> ImageColumn=1,
>
> PublicColumn=2,
>
> UsersColumn=3
>
> };
>
> ImageListProxyModel(QObject *parent=0);
>
>  virtual QModelIndex index(int row, int column,
>
> const QModelIndex &parent=QModelIndex()) const;
>
> virtual QModelIndex parent(const QModelIndex &index) const;
>
> virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
>
> virtual int columnCount(const QModelIndex &parent=QModelIndex()) const;
>
> virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
>
> virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
>
> virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole)
>
> const;
>
> };
>
> #endif // IMAGELISTPROXYMODEL_H
>

Source:

> #include "imagelistproxymodel.h"
>
> #include <QtGui>
>
> #include "imagelistmodel.h"
>
> /**
>
> \class ImageListProxyModel Proxy model for view the data from the
>
> QSqlTableView with the Image view.
>
> */
>
> /**
>
> Creates an empty \e ImageListProxyModel
>
> \param parent The Object parent.
>
> */
>
> ImageListProxyModel::ImageListProxyModel(QObject *parent) :
>
> QAbstractProxyModel(parent)
>
> {}
>
> /**
>
> Returns a index for the \e row and \e column. \e parent is always
> QModelIndex()
>
> because this is an 2D data model.
>
> \param row The row of the data model.
>
> \param column The column of the data model.
>
> \return The model index associated to this model.
>
> */
>
> QModelIndex ImageListProxyModel::index(int row, int column,
>
> const QModelIndex &parent) const
>
> { return createIndex(row, column); }
>
> /**
>
> Returns QModelIndex() for this model.
>
> \param index The model index.
>
> \return QModelIndex() because is a 2D model.
>
> */
>
> QModelIndex ImageListProxyModel::parent(const QModelIndex &index) const
>
> { return QModelIndex(); }
>
> /**
>
> Gets the row count. Simply gets the row count from the source model.
>
> \param parent The model index. Should be QModelIndex().
>
> \return The number of rows of the source model.
>
> */
>
> int ImageListProxyModel::rowCount(const QModelIndex &parent) const
>
> {
>
> int rows=sourceModel()->rowCount(parent);
>
>  qDebug()<<"rows:"<<rows;
>
> return rows;
>
> }
>
> /**
>
> Returns the number of columns for the children of the given \e parent.
>
> \param parent The model index. Should be QModelIndex().
>
> \return The number of columns -1 of the source model. Because the \e
> thumbnail
>
> column is merged in the Qt::DecorationRole of the \e name column.
>
> */
>
> int ImageListProxyModel::columnCount(const QModelIndex &parent) const
>
> { return sourceModel()->columnCount(parent)-1; }
>
> /**
>
> Returns the model index in the source model that corresponds to the
>
> \e proxyIndex in the proxy model.
>
> \param proxyIndex The proxy model index.
>
> \return The model index in the source model.
>
> */
>
> QModelIndex ImageListProxyModel::mapToSource(const QModelIndex &proxyIndex)
>
>
> const
>
> {
>
> Q_ASSERT(proxyIndex.isValid());
>
> qDebug()<<"QModelIndex ImageListProxyModel::mapToSource(const QModelIndex
> &proxyIndex)";
>
> int row=proxyIndex.row(), col=proxyIndex.column();
>
>  switch(col) {
>
> case IdColumn:
>
> return sourceModel()->index(row, ImageListModel::IdColumn);
>
> case ImageColumn:
>
> return sourceModel()->index(row, ImageListModel::NameColumn);
>
> case PublicColumn:
>
> return sourceModel()->index(row, ImageListModel::PublicColumn);
>
> case UsersColumn: return QModelIndex();
>
> }
>
> qWarning()<<"No column selected";
>
> return QModelIndex();
>
> }
>
> /**
>
> Returns the model index in the proxy model that corresponds to the
>
> \e sourceIndex from the source model.
>
> \param sourceIndex The index in the source model.
>
> \return A index in this model.
>
> */
>
> QModelIndex ImageListProxyModel::mapFromSource(const QModelIndex
> &sourceIndex)
>
> const
>
> {
>
> int row=sourceIndex.row(), col=sourceIndex.column();
>
>  switch(col) {
>
> case ImageListModel::IdColumn: return index(row, IdColumn);
>
> case ImageListModel::ThumbnailColumn:
>
> case ImageListModel::NameColumn: return index(row, ImageColumn);
>
> case ImageListModel::PublicColumn: return index(row, PublicColumn);
>
> }
>
> qWarning()<<"No column selected";
>
> return QModelIndex();
>
> }
>
> QVariant ImageListProxyModel::data(const QModelIndex &index, int role)
> const
>
> {
>
> QAbstractItemModel *m=sourceModel();
>
>  Q_ASSERT(m->metaObject()->className()=="ImageListModel");
>
>  QModelIndex srcIdx=mapToSource(index);
>
>  return srcIdx.data(role);
>
> }
>

The problem is, the rowCount is returning more than 0 rows but no row is
displayed in the table view. Can anyone helps me?

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090119/eb477a2e/attachment.html 


More information about the Qt-interest-old mailing list