Results 1 to 7 of 7

Thread: Can't style QHeaderView::section:selected in QSS stylesheet

  1. #1
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Can't style QHeaderView::section:selected in QSS stylesheet

    The title says it all: I seem unable to change the background color (or any other property) of a QHeaderView section using the QHeaderView::section:selected selector using Qt 4.7.0. Here's the relevant code:

    Qt Code:
    1. QHeaderView::section:selected
    2. {
    3. background-color: red;
    4. }
    To copy to clipboard, switch view to plain text mode 
    This is what I have now:

    QHeaderViewSectionSelectedBefore.png

    And this is what I would like to achieve (the selected section is highlighted):

    QHeaderViewSectionSelectedAfter.png

    According to the documentation, this ought to be possible:

    QHeaderView: Supports the box model. The sections of the header view are styled using the ::section sub control. The section Sub-control supports the :middle, :first, :last, :only-one, :next-selected, :previous-selected, :selected, and :checked pseudo states.
    Am I doing it right?

    (I couldn't find anything on that subject in the bug database, and I don't have Qt 4.8 handy to check if the behavior is different.)

    Cheers,
    Franz
    Last edited by dictoon; 15th January 2012 at 13:51.

  2. #2
    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: Can't style QHeaderView::section:selected in QSS stylesheet

    Try the QHeaderView::section:checked selector... the section headers are like toggling push buttons. The :selected options seem more about formatting tab bars or lists (and nothing to do with selections).

  3. #3
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't style QHeaderView::section:selected in QSS stylesheet

    Thanks for the suggestion. Unfortunately using the QHeaderView::section:checked selector has no effect either...

  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: Can't style QHeaderView::section:selected in QSS stylesheet

    Works fine here:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "connection.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. if (!createConnection())
    10. return 1;
    11.  
    12. model.setTable("person");
    13. model.select();
    14.  
    15. v.setStyleSheet( "QHeaderView::section:checked { background-color: green; }");
    16. v.setModel(&model);
    17. v.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    displays:
    demo.png
    It also works if you apply the style sheet to just the horizontal or vertical header.

  5. #5
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't style QHeaderView::section:selected in QSS stylesheet

    No luck here. Here's my test app, derived from yours but without the SQL stuff:

    Qt Code:
    1. #include <QApplication>
    2. #include <QStringList>
    3. #include <QTreeWidget>
    4. #include <QTreeWidgetItem>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. v.setColumnCount(2);
    11. v.setStyleSheet("QHeaderView::section:checked { background-color: green } QHeaderView::section:hover { background-color: red }");
    12. v.setSortingEnabled(true);
    13. v.addTopLevelItem(new QTreeWidgetItem(&v, QStringList() << "John" << "Doe"));
    14. v.show();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Setting QHeaderView::section:hover works but setting QHeaderView::section:checked doesn't.

    I also tried with QTableView as you did in your test, same problem.

    Qt 4.7.0 on Windows Vista.

    Thanks,
    Franz

  6. #6
    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: Can't style QHeaderView::section:selected in QSS stylesheet

    Linux and Qt 4.7.4: QTreeWidget does not work, QTableWidget does if the table has content.

    Highlighting is part of the style... perhaps Windows Vista style does not honour it? I don't have a Vista box hand to try on.

  7. The following user says thank you to ChrisW67 for this useful post:

    dictoon (16th January 2012)

  8. #7
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't style QHeaderView::section:selected in QSS stylesheet

    I reported a bug: https://bugreports.qt.nokia.com/browse/QTBUG-23689.

    Thanks all for your help.

    Cheers,
    Franz

Similar Threads

  1. Highlighting selected section on QDateTimeEdit
    By Yorma in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2013, 12:03
  2. Replies: 2
    Last Post: 10th August 2011, 07:16
  3. Stylesheet of selected item in combo-box
    By shrawan0786 in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2010, 10:30
  4. QHeaderView: background of selected header item with CSS
    By antarctic in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2009, 07:37
  5. QHeaderView section resizes
    By dimuz in forum Qt Programming
    Replies: 7
    Last Post: 11th October 2007, 16:44

Tags for this Thread

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.