cssqtpyqt5qtableview

What are the options available within a Qt Stylesheet for a QTableView widget?


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:

  1. https://doc.qt.io/qt-5/stylesheet-reference.html
  2. QTableView/QTableWidget grid stylesheet - grid line width
  3. How to set stylesheet for the current item in QTableView

I am simply trying to get a detailed (complete) list of stylesheet options for the QTableView. Where is this documentation? It seems strange that I have to get bits of information from all over the web to piece together a complete stylesheet.

Here is an example of the stylesheet I'm using right now:

QTableView {
    color: black;
    gridline-color: black;
    border-color: rgb(242, 128, 133);
    font: 10px;
}
QHeaderView::section {
    background-color: rgb(71, 153, 176);
    color: white;
    height: 35px;
    font: 14px;
}
QTableView::item:focus{
    border: 2px solid rgb(242, 128, 133);
    background-color: rgb(255, 254, 229);
}
QScrollBar:vertical {
    background: rgb(188, 224, 235);
}
 QScrollBar::handle:vertical {
    background: rgb(71, 153, 176);
 }
QScrollBar:horizontal {
    background: rgb(188, 224, 235);
}
 QScrollBar::handle:horizontal {
    background: rgb(71, 153, 176);
 }

This is what I still can't accomplish with the stylesheet:

  1. I would like to change the color of cells when they are selected in groups.
  2. This stylesheet has hidden the "rows" on my table
  3. I would like to edit the stylesheet for the rows as well.

Here you can see the cell behavior when selected in group: enter image description here 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.


Solution

  • 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 width or height (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;
    }
    

    Note: font sizes should theoretically be set in points, so consider changing to font: 14pt. Still, this may have different results depending on the OS and configuration (especially considering custom font scaling settings).

    Unfortunately, setting sizes with QSS has two drawbacks:

    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:

    1. Setting the ::item pseudo-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.

    2. Setting the item view selector and the selection-background-color property:

      QTableView {
          ...
          selection-background-color: rgb(242, 128, 133);
      }
      

      The above will set the table Highlight palette role, which will then revert to the default style painting, providing any "fancy" drawing used by the style.

    Note: styles use palette colors as a reference, and sometimes they completely ignore them by using custom (and often pixmap based) painting. As said above, the selection-background-color approach still relies a lot on the style, which means that the painting will be partially (or completely) overridden by the style, potentially ignoring or altering the selected color. If the selection color is more important than application/system style consistency, you should use the ::item:selected selector.