PDA

View Full Version : How customize items of ModelView?



istdasklar
28th March 2007, 16:57
Hello,
I'm french, sorry for my english.

I have a QSqlTableModel and a QTableView.

I want to customize items in my tableView.

Exemple, change the background color of a selected items with values 69:


QModelIndex id = tableView->currentIndex();
if ( model->data(id,Qt::DisplayRole) == 69 )
id->setBackground ( pink) ..... //SOMETHING THAT

Or , I want an icons near items with values = 69.

jpn
28th March 2007, 17:24
Exemple, change the background color of a selected items with values 69:



QModelIndex id = tableView->currentIndex();
if ( model->data(id,Qt::DisplayRole) == 69 )
id->setBackground ( pink) ..... //SOMETHING THAT

Subclass QSqlTableModel and reimplement QAbstractItemModel::data():


QVariant MySqlTableModel::data(const QModelIndex& index, int role) const
{
QVariant val = QSqlTableModel::data(index, role);
if (role == Qt::BackgroundRole && val == 69)
{
return QVariant::fromValue(QBrush(Qt::red)); // or QColor?
}
return val;
}




Or , I want an icons near items with values = 69.
Similar approach works. Use QModelIndex::sibling() to test sibling items' values.

wysota
28th March 2007, 18:04
Another possibilities are to use a proxy model that will do that (reimplementing data() as suggested) or a custom item delegate that will paint the item in a different way (changing the background colour or adding an icon).

Andrew
28th March 2007, 18:08
if instead of the QTableView would be a QListView what are the posibilities to change an item position ?

for example, when the mode in the list is Free you can move the items around in the widget so this tells me that items can be placed in a different position.

what i want to achieve to make all the items in a QListView \ QListWidget to be displayed at an offset from the default position ( i want to make them appear more to the right and down)

{ anwers to this post were moved to: custom painting in QListWidget (http://www.qtcentre.org/forum/f-qt-programming-2/t-custom-painting-in-qlistwidget-6310.html) }