PDA

View Full Version : Can't style QHeaderView::section:selected in QSS stylesheet



dictoon
15th January 2012, 13:32
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:


QHeaderView::section:selected
{
background-color: red;
}
This is what I have now:

7269

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

7270

According to the documentation (http://developer.qt.nokia.com/doc/qt-4.7/stylesheet-reference.html), 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

ChrisW67
15th January 2012, 23:18
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).

dictoon
16th January 2012, 07:49
Thanks for the suggestion. Unfortunately using the QHeaderView::section:checked selector has no effect either...

ChrisW67
16th January 2012, 08:28
Works fine here:


#include <QtGui>
#include <QtSql>
#include "connection.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

if (!createConnection())
return 1;

QSqlTableModel model;
model.setTable("person");
model.select();

QTableView v;
v.setStyleSheet( "QHeaderView::section:checked { background-color: green; }");
v.setModel(&model);
v.show();

return app.exec();
}

displays:
7278
It also works if you apply the style sheet to just the horizontal or vertical header.

dictoon
16th January 2012, 12:42
No luck here. Here's my test app, derived from yours but without the SQL stuff:


#include <QApplication>
#include <QStringList>
#include <QTreeWidget>
#include <QTreeWidgetItem>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QTreeWidget v;
v.setColumnCount(2);
v.setStyleSheet("QHeaderView::section:checked { background-color: green } QHeaderView::section:hover { background-color: red }");
v.setSortingEnabled(true);
v.addTopLevelItem(new QTreeWidgetItem(&v, QStringList() << "John" << "Doe"));
v.show();

return app.exec();
}
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

ChrisW67
16th January 2012, 22:39
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.

dictoon
17th January 2012, 16:30
I reported a bug: https://bugreports.qt.nokia.com/browse/QTBUG-23689.

Thanks all for your help.

Cheers,
Franz