Hello folks,
I am using Qt 4.8 and I am wondering if it possible to return an image from a custom model's QAbstractItemModel::data()-implementation based on a 'selected' row state.
I have implemented the method data() following, where MyTreeModel is derived from QAbstractItemModel
if (!index.
isValid()) return QVariant();
// Handle some other roles...
if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
// Is there any way to determine whether index.row() is selected in the view
// in order to return an other image, e.g.
// return IS_ROW_SELECTED(index) ? QImage(":/icons/gears_selected") : QImage(":/icons/gears_unselected");
//
return QImage(":/icons/gears_selected");
}
}
QVariant MyTreeModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant();
// Handle some other roles...
if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
// Is there any way to determine whether index.row() is selected in the view
// in order to return an other image, e.g.
// return IS_ROW_SELECTED(index) ? QImage(":/icons/gears_selected") : QImage(":/icons/gears_unselected");
//
return QImage(":/icons/gears_selected");
}
return QVariant();
}
To copy to clipboard, switch view to plain text mode
Thanks in advance and best regards,
michael
By playing around I finally figured out the solution - just return a QIcon instead of a QPixmap
if (!index.
isValid()) return QVariant();
// Handle some other roles...
if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
// Simply return an icon with pixmaps for different modes, then the QTreeView will decide
// which icon to render. In order to have ensured that the icon has a defined size, call QTreeView::setIconSize()
icon.
addPixmap(QPixmap(":/icons/gears_unselected"),
QIcon::Normal);
icon.
addPixmap(QPixmap(":/icons/gears_selected"),
QIcon::Selected);
return icon;
}
}
QVariant MyTreeModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant();
// Handle some other roles...
if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
// Simply return an icon with pixmaps for different modes, then the QTreeView will decide
// which icon to render. In order to have ensured that the icon has a defined size, call QTreeView::setIconSize()
QIcon icon;
icon.addPixmap(QPixmap(":/icons/gears_unselected"), QIcon::Normal);
icon.addPixmap(QPixmap(":/icons/gears_selected"), QIcon::Selected);
return icon;
}
return QVariant();
}
To copy to clipboard, switch view to plain text mode
Regards, michael
Bookmarks