I have hunted high and low for detailed documentation on the stylesheets for Qt widgets - specifically the QTableView widget. Here were some helpful (although incomplete) resources I found:
I want the blue cells to match the style of the pink outlined cell.
What are the options for a QTableView widget? I have tried QTableView::rows, QTableView::selection, and many others to no luck.
CodePudding user response:
An important aspect to consider when dealing with QSS (Qt Style Sheets) is that when setting any property on complex widgets, all other basic properties must be set.
The documentation is clear about "common" widgets (like QComboBox or QScrollBar) but not about properties of more problematic widgets like QHeaderView (which is the widget responsible of showing the section title of rows or columns).
Most importantly, strictly related properties like
widthorheight(which are not supported for all widgets) must both be set.If you want to set a specific height for header sections in stylesheets, you must set the width too.
QHeaderView::section { background-color: rgb(71, 153, 176); color: white; height: 35px; width: 150px; font: 14px; }Unfortunately, setting sizes with QSS has two drawbacks:
- the size is fixed and based on the "pixel" size;
- text elision is automatically disabled (at least with QHeaderView and with normal styles);
This brings us to an important aspect: stylesheets must be used with care (and the only way to be aware of that is experience and studying of the sources). Setting explicit sizes in stylesheets is almost always discouraged, especially when those sizes deal with text displaying. If you want to set a default dimension for the header, you should use the
setDefaultSectionSize()instead.
Finally, even if the problem was solved by the OP, I'll add the following just for clarity.
Selection colors for item views can be set in two different ways:Setting the
::itempseudo-selector color:QTableView::item:selected { background-color: rgb(242, 128, 133); }The above will set the background of the item and completely override the style painting behavior (depending on the style), including any further "fancy" drawing that is based on the palette. Simply put, it will probably be a plain background color.
Setting the item view selector and the
selection-background-colorproperty:QTableView { ... selection-background-color: rgb(242, 128, 133); }The above will set the table
Highlightpalette role, which will then revert to the default style painting, providing any "fancy" drawing used by the style.
