Results 1 to 6 of 6

Thread: Change appearence of selected row in QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    Finrond (15th February 2013)

  3. #2
    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

  4. #3
    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 15:53.

  5. 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, 12:49
  2. How to change columns order in a QTableView...
    By cydside in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2009, 10:42
  3. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  4. QGraphicsView : change selected rectangle style
    By kghose in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 18:12
  5. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 14: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
  •  
Qt is a trademark of The Qt Company.