[Python] How to return a QColor as the QBackgroundRole in QTableView which has preset...

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I encountered a strange problem as stated in the title.

    I have a QTableView.

    self.table = QTableView()


    Then set some QSS to it.

    self.table.setStyleSheet('''
    QTableView {
    border: 2px solid red;
    padding: 5px;
    border-radius: 5px;
    gridline-color: red;
    color: red;
    }
    QTableView::item{
    border-color: none;
    padding-left: 5px;
    padding-right: 5px;
    gridline-color: rgb(44, 49, 60);
    border-bottom: 1px solid green;
    }
    QTableView::item:selected{
    background-color: blue;
    }
    ''')


    Then I try to return a QColor inside the model (QAbstractTableModel), It just won't work. If I removed the style sheet, then it worked. Does anyone which line of my QSS created the problem?

    if role == Qt.BackgroundRole and index.row() == self.headStart:
    # Set header start color
    return QColor(208, 74, 2)


    I need the stylesheet, at the same time I also need Qt.BackgroundRole to work.

    Continue reading...

Compartilhe esta Página