[SailfishDevel] Build a Model with "fixed" items + "dynamic" ones

Nils Jeisecke jeisecke at saltation.de
Fri Apr 17 10:59:22 UTC 2015


Hi,

On Fri, Apr 17, 2015 at 12:09 PM, François K. <daitheflu at free.fr> wrote:
> 1/ The first one is to build a Model that would inherit from QAbstractListModel.
> In the constructor, I would add the first 6 items (hard-code them).

maybe this code can give you some inspiration to solve at least some
part of your problem:

-------------- prefixmodel.h

#ifndef PREFIXMODEL_h
#define PREFIXMODEL_h

#include <QAbstractProxyModel>

class RealModel;

class PrefixModel : public QAbstractProxyModel
{
  Q_OBJECT

public:

  explicit PrefixModel(QObject *parent = 0);

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

  QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
  QModelIndex mapToSource(const QModelIndex &proxyIndex) const;

  void setRealModel(RealModel *sourceModel)

private slots:

  void sourceRowsInserted(const QModelIndex &, int, int);
  void sourceRowsRemoved(const QModelIndex &, int, int);
  void sourceRowsAboutToBeInserted(const QModelIndex &, int, int);
  void sourceRowsAboutToBeRemoved(const QModelIndex &, int, int);
  void sourceReset();
  QVariant data(const QModelIndex &proxyIndex, int role) const;
};

#endif // PREFIXMODEL_h

-------------- prefixmodel.cpp

#include "prefixmodel.h"
#incldue "realmodel.h"

#define FIX_ITEMS 6

PrefixModel::PrefixModel(QObject *parent) :
  QAbstractProxyModel(parent)
{
}

QModelIndex PrefixModel::mapFromSource(const QModelIndex &sourceIndex) const
{
  return sourceModel()->index(sourceIndex.row() + FIX_ITEMS,
sourceIndex.column());
}

QModelIndex PrefixModel::mapToSource(const QModelIndex &proxyIndex) const
{
  if (!sourceModel())
    return QModelIndex();

  return sourceModel()->index(proxyIndex.row() - FIX_ITEMS,
proxyIndex.column());
}

QVariant PrefixModel::data(const QModelIndex &proxyIndex, int role) const
{
  if (proxyIndex.row() < FIX_ITEMS) {
    if (role == Qt::DisplayRole)
      return QString("fix item #%1").arg(proxyIndex.row());

    return QVariant();
  }
  return QAbstractProxyModel::data(proxyIndex, role);
}

int PrefixModel::rowCount(const QModelIndex &parent) const
{
  if (!sourceModel())
    return 0;

  return sourceModel()->rowCount(parent) + FIX_ITEMS;
}

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

void PrefixModel::setRealModel(RealModel *sourceModel)
{
  if (this->sourceModel() == sourceModel)
    return;

  if (this->sourceModel())
    this->sourceModel()->disconnect(this);

  QAbstractProxyModel::setSourceModel(sourceModel);
  emit sourceModelChanged();

  connect(sourceModel,
SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this,
SLOT(sourceRowsAboutToBeInserted(QModelIndex,int,int)));
  connect(sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
  connect(sourceModel,
SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this,
SLOT(sourceRowsAboutToBeRemoved(QModelIndex,int,int)));
  connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this,
SLOT(sourceRowsRemoved(QModelIndex,int,int)));
  connect(sourceModel, SIGNAL(modelReset()), this, SLOT(sourceReset()));
}

RealModel *PrefixModel::sourceModelInternal() const
{
  return qobject_cast<RealModel*>(QAbstractProxyModel::sourceModel());
}

void PrefixModel::sourceRowsInserted(const QModelIndex &, int start, int end)
{
  endInsertRows();
}

void PrefixModel::sourceRowsRemoved(const QModelIndex &, int start, int end)
{
  endRemoveRows();
}

void PrefixModel::sourceRowsAboutToBeInserted(const QModelIndex &, int
start, int end)
{
  beginInsertRows(QModelIndex(), start + FIX_ITEMS, end + FIX_ITEMS);
}

void PrefixModel::sourceRowsAboutToBeRemoved(const QModelIndex &, int
start, int end)
{
  beginRemoveRows(QModelIndex(), start + FIX_ITEMS, end + FIX_ITEMS);
}

void PrefixModel::sourceReset()
{
  beginResetModel();
  endResetModel();
}

Nils


More information about the Devel mailing list