QTableView with icons in rows
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.
Re: QTableView with icons in rows
I would use a QStyledItemDelegate
If you subclass it you can reimplement the paint Method to paint the icon.
Re: QTableView with icons in rows
This is my question too, but the QSqlTableModel ignores every role other than DisplayRole and EditRole. why?
Re: QTableView with icons in rows
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.
Re: QTableView with icons in rows
Quote:
either by setting that data yourself
but the output of setData in the following code is false:
Code:
ui
->tblv_table
->model
()->setData
( ui
->tblv_table
->model
()->index
(row, col,
QModelIndex() ),
QIcon("icon.png"), Qt
::DecorationRole);
tblv_table is a QTableView
Re: QTableView with icons in rows
You could try setIconSize
Otherwise you will have to try a QStyledItemDelegate, I guess
Re: QTableView with icons in rows
Quote:
Originally Posted by
.:saeed:.
but the output of setData in the following code is false:
Code:
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).