PDA

View Full Version : tableView for files: give one type a different colour: why doesn't this work?



qt_gotcha
3rd September 2010, 17:35
using the stardelegate example I made a simple paint function to make a file with extension "001" blue:

void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
if (index.column() == 0)
{
QString name = index.model()->data(index, Qt::DisplayRole).toString();
if (QFileInfo(name).suffix() == "001")
{
painter->setPen(QColor(0, 0, 255, 255));
QFont f = painter->font();
painter->setFont(f);
opt.font.setItalic(true);
}
}
QStyledItemDelegate::paint(painter, opt, index);
}
The files with 001 are displayed in italics but not in blue. So the selection and adapting opt works, but setPen doesn't. Why not? thanks.

tbscope
3rd September 2010, 17:53
No, this doesn't work because the settings made to the painter are only local.
When the painting is done (as in being performed), the actual style is used and your changes to the painter are lost.

Unless, of course, you do the painting of the text directly in the delegate.

Like the italics, set the color in the option. Then it will work as the style makes use of the option (or should make use of it)

qt_gotcha
4th September 2010, 06:18
thanks, using this now

if (QFileInfo(name).suffix() == "001")
opt.palette.setColor(QPalette::Text,QColor(0, 0, 255, 255) );

and it works