Search code examples
qtqt5qtableviewqtstylesheets

How to set stylesheet for the current item in QTableView


When QTableView edit control is visible for the current item the shylesheet of the edit takes place. When there is no active edit control in the QTableView the current item is styled using the QTableView { selection-background-color: } How to set different style only for the current item?

Current item visible only when QTableView has focus Active edit-box on the current item


Solution

  • Qt style sheets support sub-controls and pseudo states, you can use it to improve your customization. see Qt6 docs

    In this case you can use the ::item sub-control and the :focus pseudo state (the "current" pseudo state doesn't exist, but the :focus does the same).

    This is an example that you can use:

    QTableView::item:focus
    {
       selection-background-color: yellow;
    }
    

    enter image description here

    See also Qt6 documentation of customizing a qtableview