PDA

View Full Version : QTableView with icons in rows



kasper360
4th April 2011, 06:52
I have a QTableView showing rows of a database table. In this table I have a column called data type and I have icon images for each type. How can I add these icons in front of each data type?

p.s.-
I found a thread discussing a topic much similar to this. Link ->http://www.qtcentre.org/threads/9655-QTableView-icons
But I'm afraid I didn't quite understand what it said. So if someone can explain this a little bit further, it is much appreciated. Thank you.

ChiliPalmer
4th April 2011, 12:09
I would use a QStyledItemDelegate (http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html)

If you subclass it you can reimplement the paint Method to paint the icon.

.:saeed:.
4th April 2011, 12:31
This is my question too, but the QSqlTableModel ignores every role other than DisplayRole and EditRole. why?

ChiliPalmer
4th April 2011, 12:44
If you have a look at the Qt Namespace doc you will see that DisplayRole and EditRole are the same. How would a QSqlTableModel now which data to save as DecorationRole or whatever? You have to do that yourself, either by setting that data yourself or by using a Delegate to paint the Icons.

.:saeed:.
4th April 2011, 12:51
either by setting that data yourself
but the output of setData in the following code is false:


ui->tblv_table->model()->setData( ui->tblv_table->model()->index(row, col, QModelIndex() ),
QIcon("icon.png"), Qt::DecorationRole);

tblv_table is a QTableView

ChiliPalmer
4th April 2011, 14:00
You could try setIconSize (http://doc.qt.nokia.com/4.7/qabstractitemview.html#iconSize-prop)
Otherwise you will have to try a QStyledItemDelegate, I guess

ChrisW67
5th April 2011, 06:24
but the output of setData in the following code is false:


ui->tblv_table->model()->setData( ui->tblv_table->model()->index(row, col, QModelIndex() ),
QIcon("icon.png"), Qt::DecorationRole);

tblv_table is a QTableView

It is false because the underlying generic SQL database has nowhere to store the image that you have set as the Decoration role. You can either use a delegate on the view to draw the relevant icon or you could subclass QSqlTableModel and override data() to return appropriate icons when the DisplayRole is requested by the view (and pass other requests on to the base class).