PDA

View Full Version : QStandardItemModel with QIdentityProxyModel



yagabey
6th December 2014, 21:31
Hi , i have a problem with QStandardItemModel and QIdentityProxyModel.

I've implemented a QIdentityProxyModel to be able to change the way of displaying the model. Here is the data() function of QIdentityProxyModel :


QVariant data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole && index.column() == AlarmLogViewIndex_RecovTime){
qDebug()<< sourceModel()->data(index);
qint64 sinceEpoch = sourceModel()->data(index).toLongLong();
const QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(sinceEpoch);

return dateTime.toString(m_formatString);
}
}

This works like a charm when source model is a QSqlTableModel. But i need to use the same QIdentityProxyModel for a QStandardItemModel. When i use standartItemModel as the source of QIdentityProxyModel , i get QVariant(Invalid) values from the above data() function (in qDebug output...) . I was suspicious about the way i created the StandardItemModel. But it works well without the QIdentityProxyModel .

Here is how i implement the StandardItemModel,

adding a row to the model:

void TagChannelItemModel::addToTagModel(pEtiketDef_t tg)
{
int row = tagModel->rowCount();

QStandardItem* item = new QStandardItem(QString::number(tg.Id));
tagModel->setItem(row, TgIndex_Id, item);

item = new QStandardItem(tg.Name);
tagModel->setItem(row, TgIndex_Name, item);

item = new QStandardItem(QString::number(tg.PollFrequency));
tagModel->setItem(row, TgIndex_PollFreq, item);
}

Should i do anything else for this item model to work with ProxyModel ?

Thanks in advance...

anda_skoa
7th December 2014, 10:43
You are passing a wrong index to the source model, an index of the proxy instead of an index of the source model itself.

See QAbstractProxyModel::mapToSource().

Cheers,
_

yagabey
7th December 2014, 13:36
Hi, thanks you were right.. It works now.. Shouldn't the indexes be same since it is "identity" proxt model ?

anda_skoa
7th December 2014, 23:50
Hi, thanks you were right.. It works now.. Shouldn't the indexes be same since it is "identity" proxt model ?

No. They will just have the same structure, but a QModelIndex is always directly tied to the model that created it.

There is usually very little difference when models are list or tables, since the cell can be addressed unambiguously through row and column, but once you have a tree structure, the model needs to store some model specific data in the index object to know which subtree it is working on.

QStandardItemModel is capable of being a tree model, so it does that and will then try to retrieve that additional data when getting an index as a function argument.
It very likely even checks if index.model() is equal to itself before attempting to do anything with an index.

Your test with the SQL table model worked by chance, because as a table it doesn't need to be as careful (it should though)

Cheers,
_