Results 1 to 6 of 6

Thread: QHeaderView header width and selected background color

  1. #1
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QHeaderView header width and selected background color

    I have two questions that are both related to the QHeaderView for a QTableView:

    1. How can I set the vertical header width? I can use the model to set the data to " " or some other character but i would prefer to set a fixed pixel width. Setting a minimum size for the horizontal header does not seem to have any effect on the vertical header's width.

    2. I have given each vertical header a background color. This color is overridden when the row is selected, provided setClickable() has been set to True. How can i override the selected header item's background color using the QPalette?

    Thank you for your time,
    TR

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView header width and selected background color

    You could try to use QSS, see Customizing QHeaderView.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView header width and selected background color

    I'll try that! If i can't use QSS, is there any other way?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHeaderView header width and selected background color

    QHeaderView::setDefaultSectionSize() and QHeaderView::resizeSection() called on the verticalHeader() of the view. You might also want to set the resizeMode().

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView header width and selected background color

    For the first option you can use for instance:
    Qt Code:
    1. ...
    2. tableView->verticalHeader()->setMinimumWidth(100);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    The second option is a bit harder to implement. You will probably need to reimplement QHeaderView::paintSection.
    Because of drawing section is based on a current style (QStyle) changing palette of a header can't give you expected result.

    So, we don't you simply user QSS as I suggested before, it works fine for the second option.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    TheRonin (24th October 2013)

  7. #6
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView header width and selected background color

    Using QSS worked for controlling the selection color but with the Style i'm using i had to override the regular QHeaderView::section with a style-sheet as well.

    In my case i need to vary the color depending on what state my application is in, so constantly switching the stylesheet might be ineffective in that regard. I managed to override paintSection per your recommendation and can thereby paint the section with a dynamic color. As you mentioned, i could not simply change the palette and had to draw the rectangle and text manually. It doesn't look as nice as I would like but might be good enough in this case. Here is the code:

    Qt Code:
    1. class RowHeader : public QHeaderView
    2. {
    3. public:
    4. void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    5. {
    6. QModelIndexList selectedRows = this->selectionModel()->selectedRows();
    7. foreach (const QModelIndex& rowIndex, selectedRows){
    8. if (rowIndex.row() == logicalIndex){
    9. QBrush brush = qvariant_cast<QBrush>(rowIndex.data(Qt::BackgroundRole));
    10. QString text = qvariant_cast<QString(rowIndex.data(Qt::DisplayRole));
    11.  
    12. painter->save();
    13. painter->setBrush(brush.color().darker());
    14. painter->drawRect(rect);
    15. painter->restore();
    16. painter->drawText(rect, Qt::AlignCenter, text);
    17. return;
    18. }
    19. }
    20. QHeaderView::paintSection(painter, rect, logicalIndex);
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    Thanks to everyone who responded to the thread!

Similar Threads

  1. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  2. Replies: 2
    Last Post: 17th July 2010, 21:07
  3. Replies: 1
    Last Post: 11th September 2009, 17:03
  4. background color of not selected tab
    By iridium in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2009, 15:57
  5. QHeaderView: background of selected header item with CSS
    By antarctic in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2009, 07:37

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.