Hi,
after many unsuccessful tests with Qt::BackgroundRole and setData() I've switched to the delegate option. And, if I had knew how easy it is to use this possibility I had saved 3 days of frustration ...
For any newbies who want to know how this works, here's my implementation:
First, I delete all stuff with setData ...
Second, I've overwritten the paint() function of QStyledItemDelegate:
@void BackgroundColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.data().isValid())
{
QStyleOptionViewItem viewOption(option);
painter->save();
painter->fillRect(option.rect, QColor(238, 233, 233, 255));
painter->restore();
viewOption.palette.setColor(QPalette::Text, QColor(Qt::black));
QStyledItemDelegate::paint(painter, viewOption,index);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
@
(found it here: "QTableView and QTreeView item background":http://permalink.gmane.org/gmane.comp.lib.qt.general/29846 and here: "Change appearence of selected row in QTableView":http://www.qtcentre.org/threads/21482-Change-appearence-of-selected-row-in-QTableView )
Third, I create the delegate object:
@BackgroundColorDelegate* m_background_delegate;@
@m_background_delegate = new BackgroundColorDelegate;@
Fourth, I activate the custom delegate every time I need it (normally in the view mode) else the default delegate will use:
@void VdBmtcLearn::changeBgOfSelectedLabelsInView(bool bg_change)
{
QList<int> indexes_of_labels = m_controller->getIndexesOfFrameLabels(m_actual_frame.viewed_labels_list);
for (int i=0; i<indexes_of_labels.length(); i++)
{
QModelIndex sourceIndex = m_model->index(indexes_of_labels[i], 0);
QModelIndex viewIndex = getViewIndex(sourceIndex);
if (bg_change)
m_view->m_table->setItemDelegateForRow(viewIndex.row(), m_view->m_background_delegate);
else
m_view->m_table->setItemDelegateForRow(viewIndex.row(), new QStyledItemDelegate);
}
}
@
(found how to call the custom delegate here: "Show other data in QTableView with QItemDelegate":http://stackoverflow.com/questions/2013052/show-other-data-in-qtableview-with-qitemdelegate )
Thanks all for helping anyway ;-)
Cheers,
Thomas