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

BackgroundRole color change not taking effect in QTableView with stylesheet



  • Good morning,

    I have created a TableView that uses a model that sets the Background Role to a yellow color when a warning is associated with a certain element contained in the model. It works fine except when I apply a stylesheet.

    QTableView {
      qproperty-showGrid: false;
      qproperty-alternatingRowColors: false;
      color: rgb(216, 217, 218);
      border: 1px solid transparent;
      border-radius: 5px;
      background-color: rgb(45, 50, 54);
      selection-color: rgb(172, 0, 122);
      selection-background-color: transparent;
      margin: 10px;
      outline: none;
    }
    
    QTableView::item {
      border: 1px solid rgb(64, 66, 62);
      border-top-color: transparent;
      border-right-color: transparent;
      border-left-color: transparent;
      padding-left: 20px;
    }
    

    The graphic effect you see is that the cell has a different background than the one set by changing the BackgroundRole.

    Perhaps this is not the right way to dynamically style a table's cells. Can you suggest me the correct approach to solve my problem?


  • Lifetime Qt Champion

    @cdemuru said in BackgroundRole color change not taking effect in QTableView with stylesheet:

    It works fine except when I apply a stylesheet.

    As described in the documentation: "Note: If Qt Style Sheets are used on the same widget as functions that set the appearance of widgets, such as QWidget::setFont() or QTreeWidgetItem::setBackground(), style sheets will take precedence if the settings conflict."

    Use a custom QStyledItemDelegate.


Log in to reply