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

Setting CSS Padding Overrides BackgroundRole



  • I'm not sure if it's actually being overridden or if it's just defaulting to white, but, I have a model that returns a specific background role for certain items, but when setting the left padding of the table items using:

    QTableView::item {
        padding-left: 20px;
    }
    

    The color I set using the BackgroundRole becomes white.

    Do I have to implement a custom delegate just to add padding?


  • Lifetime Qt Champion

    @Qt_User72653
    But is it not because you add padding and that is not part of
    the area painted with background (content) ?
    Just wondering. Not sure what constitutes "background" for an item.

    alt text



  • I made an error in the CSS I used; I need to set a border for the padding to work:

    QTableView:item {
        border: none;
        padding-left: 20px;
    }
    

    I also tried just setting the left border, thinking that I might be able to use that to simulate padding, but it completely removes the background color, too, using:

    QTableView::item {
      border-left: 10px solid transparent;
    }
    

    @mrjj said in Setting CSS Padding Overrides BackgroundRole:

    @Qt_User72653
    But is it not because you add padding and that is not part of
    the area painted with background (content) ?
    Just wondering. Not sure what constitutes "background" for an item.)

    I could understand if the background was only blank where the padding would be, but when setting the padding through CSS it completely removes the background color.


  • Lifetime Qt Champion

    If wondering if you are seeing the viewport().
    When you say completely remove background , it means all for the
    the area or just the pad part ?

    Anyway, i just realized that setting Stylesheet on widgets, disables the normal drawing.
    Im wondering if that also applies to BackgroundRole handling for *views.



  • Yeah, it completely removes the item background.

    Is there a simpler way to do this, or do I have to implement a custom delegate?



  • We stumbled over this recently, seems to be a bug. No matter what kind of property you set for an item in the CSS, the background role becomes useless.


  • Lifetime Qt Champion


Log in to reply