Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom QTableView Stylesheet



  • I am using Qt 5.15. My application allows the user to switch between 3 different themes (none, dark, and light). When switching between themes one of my custom QTableViews stops using the selected stylesheet. This only happens when switching; upon application startup, it retains the proper last used theme's stylesheet. Other QTreeViews and/or QTableViews used in the application do not have this issue.

    Sample stylesheet contains:

    QAbstractItemView {
        background-color: #414147;
        alternate-background-color: #545459;
    }
    

    Even explicitly using:

    CustomTableView {
        background-color: #414147;
        alternate-background-color: #545459;
    }
    

    will still resort to the same odd behavior. Any suggestions on what I can look for to determine why the custom QTableView doesn't behave like the others?


  • Lifetime Qt Champion

    Two things. First check if QEvent::StyleChange is sent to your custom widget. Second see if it works when you explicitly call qApp->polish(customWidgetPtr) after you set the new app style.



  • Thanks for the info.

    There are times when QEvent::StyleChange was not received and of course the stylesheet was not correctly applied. But there were also times when it was received but the custom table view still ignored the stylesheet. (I printed the stylesheet each time the event was received.)
    I wasn't sure where I was supposed to make the polish call so I didn't get any additional information from this.

    Sometimes it's the rows that are not following the stylesheet and sometime it's the header that is not following the stylesheet. It is not consistent.

    Here's the table when there's no stylesheet selected.
    8950b105-0857-45a2-82cb-81528cac28c8-image.png

    Here the table ignored the background color and text color of the row it should be black with white text but the header section is correct.
    b3555f3f-588b-496a-8491-6a4b233d5b65-image.png

    Here the table ignored the background color of the row (it should be yellow) and the header section is correct.
    e7a9f70c-2eb3-44d6-adcc-1688a8043cc5-image.png

    Here the background color and text color of the row are correct but the header section is incorrect, the header section should be black with white text.
    9904df6c-3ed5-46ec-8a10-e70e1cf8351e-image.png

    Here the background color and text color of the row are correct but the header section is incorrect, the header section should be yellow with black text.
    3f258c0a-30dd-4e9c-82b9-e4d6971e2d1f-image.png


Log in to reply