Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. BackgroundRole color change not taking effect in QTableView with stylesheet
Qt 6.11 is out! See what's new in the release blog

BackgroundRole color change not taking effect in QTableView with stylesheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cdemuruC Offline
    cdemuruC Offline
    cdemuru
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved