Creating own proxy
-
Hello,
My purpose here is to create a proxy destinated to combobox.
Basically, I would like to have a list of items into my combobox. But as a first item, I would like to see appearing "all" instead of the first element of my list.
For that purpose, I thought to develop my own proxy by inheriting QAbstractProxyModel. Its name in the following code is FilterComboboxModel
What I have here is actually working fine but whenever the user of my proxy is calling my function mapToSource, I'm getting the following warning:
QSortFilterProxyModel: index from wrong model passed to mapToSource.The hierarchy of my models is done as following:
@
MyModel categoryListModel = MyModel();
categoryFilter_m = new MyFilterModel(this);
categoryFilter_m->setSourceModel(&categoryListModel);
FilterComboBoxModel *categoryCbModel = new FilterComboBoxModel(this);
categoryCbModel->setSourceModel(categoryFilter_m);
this->widget.filterCategoryComboBox->setModel(categoryCbModel);
@Here is the code of my class. I'm trying to give what I believe interesting but feel free to ask more code if you have additional questions:
Does anybody would know what is wrong?
@
QVariant FilterComboBoxModel::data(const QModelIndex & index, int role = Qt::DisplayRole) const {
if (!index.isValid()) {
return QVariant();
}
if (index.row() == 0) {
switch (role) {
case Qt::DisplayRole:
return QVariant("All");
case Qt::UserRole:
return QVariant::fromValue((void*) NULL);
default:
return QVariant();
}
} else {
QModelIndex subIdx = sourceModel()->index(index.row() - 1, index.column());
return sourceModel()->data(subIdx, role);
}
}QModelIndex FilterComboBoxModel::mapToSource(const QModelIndex& proxyIndex) const {
if (!proxyIndex.isValid() || !sourceModel()) {
return QModelIndex();
}
if (proxyIndex.row() == 0) {
return QModelIndex();
}
QModelIndex toSourceIdx;
QAbstractProxyModel* absSourceModel = dynamic_cast<QAbstractProxyModel*> (sourceModel());
if (absSourceModel) {
QModelIndex subIdx = absSourceModel->index(proxyIndex.row() - 1, proxyIndex.column(), proxyIndex.parent());
toSourceIdx = absSourceModel->mapToSource(subIdx);
} else {
toSourceIdx = sourceModel()->index(proxyIndex.row() - 1, proxyIndex.column());
}
return toSourceIdx;
}QModelIndex FilterComboBoxModel::mapFromSource(const QModelIndex& sourceIndex) const {
if (!sourceIndex.isValid()) {
return QModelIndex();
}
return index(sourceIndex.row() + 1, sourceIndex.column());
}QModelIndex FilterComboBoxModel::parent(const QModelIndex&) const {
return QModelIndex();
}
@Thanks a lot!
Kind regards,
Laurent