I have a map = std::map<std::string, myItemModel *>
, where myItemModel
inherits QAbstractItemModel
.
I want now to combine all myItemModel
in one single myItemModel
(every other item model would be fine too).
So that there is one big myItemModel
.
Is there a 'qt-way' to do this?
It can be done, but it's not trivial. It depends on your implementation of QAbstractItemModel
and that's why it hasn't been done in Qt.
Here are steps to implement a model which is a collection of models:
QAbstractItemModel
rowCount
and provide a sum of all models rows. columnCount
and provide a number of columns in your models. index
, return createIndex(row, column, NULL);
parent
, return QModelIndex(); I hope your models are not trees data
,setData
etc. addressing calls to the right model. Use methods from #10 to convert indexes. Example (indexes): BaseModel ChildModel1 ChildModel2 0,0 0,0 1,0 1,0 2,0 0,0 3,0 1,0 4,0 2,0
p.s. Think about creating a cache of indexes mapping.
This is an example of a method to convert a base model index to a child model index:
const QModelIndex childModelIndex(const QModelIndex& baseModelIndex) const
{
if (!baseModelIndex.isValid())
{
return QModelIndex();
}
int count = 0;
const int row = baseModelIndex.row();
for (QList<QAbstractTableModel*>::const_iterator it = m_models.begin();
it != m_models.end(); it++)
{
const int currentCount = (*it)->rowCount();
if (row >= count && row < count + currentCount)
{
return (*it)->index(row - count, 0);
}
count += currentCount;
}
ASSERT(false);
return QModelIndex();
}
This is an example of a method to convert a child model index to a base model index:
QModelIndex baseModelIndex(const QModelIndex& childModelIndex) const
{
int row = childModelIndex.row();
for (QList<QAbstractTableModel*>::const_iterator it = m_models.begin();
it != m_models.end(); it++)
{
if (childModelIndex.model() == *it)
{
return index(row, ind.column());
}
row += (*it)->rowCount();
}
return QModelIndex();
}
The KDE Frameworks project contains a module called KItemModels, which includes a class called KConcatenateRowsProxyModel. It does exactly what you want. The library is released every month as part of the [KDE Frameworks releases], the code is continuously unit tested on https://build.kde.org. All this is licensed under LGPL v2 or later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With