Results 1 to 6 of 6

Thread: Change appearence of selected row in QTableView

  1. #1
    Join Date
    Mar 2008
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Change appearence of selected row in QTableView

    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.

  2. #2
    Join Date
    Apr 2009
    Posts
    21
    Thanked 3 Times in 2 Posts

    Default Re: Change appearence of selected row in QTableView

    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

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change appearence of selected row in QTableView

    or u can write your own delegate. that will give u full control i case more customization is required

  4. #4
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Change appearence of selected row in QTableView

    Working with a delegate is a good idea as sugested by MrDeath, here is an example:

    Qt Code:
    1. void
    2. MouradDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
    3. {
    4. if (option.showDecorationSelected && (option.state & QStyle::State_Selected)){
    5. if (option.state & QStyle::State_Active)
    6. // Mourad place here whatever you would like to see when the row is selected and active
    7. painter->fillRect(option.rect, option.palette.highlight().color());
    8. else {
    9. // Mourad place here whatever you would like to see when the row is selected but not active
    10. QPalette p=option.palette;
    11. painter->fillRect(option.rect, p.color(QPalette::Inactive, QPalette::Background));
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to schall_l for this useful post:

    Finrond (15th February 2013)

  6. #5
    Join Date
    Mar 2008
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Change appearence of selected row in QTableView

    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

  7. #6
    Join Date
    Apr 2009
    Posts
    21
    Thanked 3 Times in 2 Posts

    Default Re: Change appearence of selected row in QTableView

    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.

    Qt Code:
    1. void MouradDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
    2. {
    3. QStyleOptionViewItemV4 myOption = option;
    4. myOption.text = i.data().toString();
    5.  
    6. if (option.showDecorationSelected && (option.state & QStyle::State_Selected)){
    7. myOption.font.setBold(true);//text to be bold when selected
    8. } else {
    9. myOption.font.setBold(false);//text not to be bold when unselected
    10. }
    11. //draw the cell with myOption:
    12. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by freezeblue; 4th June 2009 at 16:53.

  8. The following user says thank you to freezeblue for this useful post:

    Finrond (15th February 2013)

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  2. How to change columns order in a QTableView...
    By cydside in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2009, 11:42
  3. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:30
  4. QGraphicsView : change selected rectangle style
    By kghose in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 19:12
  5. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 15:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.