PDA

View Full Version : Change appearence of selected row in QTableView



mourad
3rd June 2009, 11:22
Hello everyone, I've a TableView in witch I want to customize the background of the selected row by rending changing the font to bold and not have the blue backgound color.
Can anyone tell me how to do.
Many thanks.
Best regurads.

freezeblue
3rd June 2009, 15:12
You can change the color role QPalette::Highlight of QTableView's palette to whatever
you want. Read QPalette doc for detail:

QPalette::Highlight
A color to indicate a selected item or the current item. By default, the highlight color is Qt::darkBlue.

As for font, Qt accepts rich text. So, you can change the text of select item's text by using setText("<b>YOURCONTENTS</b>")

Personal ideas. Just for reference:o

nish
3rd June 2009, 15:54
or u can write your own delegate. that will give u full control i case more customization is required

schall_l
3rd June 2009, 16:43
Working with a delegate is a good idea as sugested by MrDeath, here is an example:



void
MouradDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)){
if (option.state & QStyle::State_Active)
// Mourad place here whatever you would like to see when the row is selected and active
painter->fillRect(option.rect, option.palette.highlight().color());
else {
// Mourad place here whatever you would like to see when the row is selected but not active
QPalette p=option.palette;
painter->fillRect(option.rect, p.color(QPalette::Inactive, QPalette::Background));
}
}
}

mourad
4th June 2009, 14:03
Thanks for everybody for the contribution. I post with some relay because I've taken some time to read documentation of Delegate Classes and their applications and I've tried to execute the exemple.
But now, otherwise the tableview contains data but the are transparent. Just I see that there are n rows but data are transparent.
Surelly, I'm missing something or I'm wrong in some place.
I'll be very thankful if anyone can adavance in the description of this very good solution.
Many thanks in advance.
Best regards.
Mourad

freezeblue
4th June 2009, 15:56
You forgot to draw text. when using ItemDelegate, what you see in the cell is fully controlled by paint() function. You must code every thing yourself.

Base on schall_l:

First set the palette of table widget to whatever color you want as I said in #2. Although set it in paint() function is also OK, I think use Designer to set palette is more convenient.


void MouradDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
QStyleOptionViewItemV4 myOption = option;
myOption.text = i.data().toString();

if (option.showDecorationSelected && (option.state & QStyle::State_Selected)){
myOption.font.setBold(true);//text to be bold when selected
} else {
myOption.font.setBold(false);//text not to be bold when unselected
}
//draw the cell with myOption:
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}

You can learn QStyle class reference. QStyle class gives you advanced control of everything related with display.
Notice: QStyleOptionViewItemV4 is available after Qt 4.4.