UN-GCPDS / qt-material

Material inspired stylesheet for PySide2, PySide6, PyQt5 and PyQt6
https://qt-material.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
2.39k stars 253 forks source link

TableWidget.item(row,column).setBackground(color) has no effect when using apply_stylesheet #95

Open kohei-noda-qcrg opened 1 year ago

kohei-noda-qcrg commented 1 year ago

I'm a newbie to Pyside6 and qt_material.

I used the source code at the end of this comment. But self.item(row, column).setBackground(color) has no effect when I use qt_material module.

Can I resolve this issue?

https://github.com/UN-GCPDS/qt-material/issues/55#issuecomment-1126977849 (add custom-stylesheets) did not work in my case.

https://github.com/UN-GCPDS/qt-material/assets/103017367/a8242332-91be-4cb1-be51-9f4db829c2cf

https://github.com/UN-GCPDS/qt-material/assets/103017367/80ec112b-e90a-4cb6-b01d-5640bd072a33

class TableWidget(QTableWidget): def init(self): super().init() self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.show_context_menu) self.load_output("data.out")

def load_output(self, file_path):
    with open(file_path, newline='') as output:
        out = output.readlines()
        # output is space separated file
        rows = [line.split() for line in out]
        len_row = len(rows)
        len_column = max(len(row) for row in rows) if len_row > 0 else 0
        self.setRowCount(len_row)
        self.setColumnCount(len_column)
        for row in range(len_row):
            for column in range(len_column):
                try:
                    text = rows[row][column]
                except IndexError:
                    text = ""
                item = QTableWidgetItem(text)
                self.setItem(row, column, item)

def show_context_menu(self, position):
    menu = QMenu()
    pale_blue_action = QAction("Pale Blue", self)
    pale_green_action = QAction("Pale Green", self)
    pale_pink_action = QAction("Pale Pink", self)
    pale_yellow_action = QAction("Pale Yellow", self)
    pale_blue_action.triggered.connect(lambda: self.change_background_color(QColor("#D3E8EB")))
    pale_green_action.triggered.connect(lambda: self.change_background_color(QColor("#D5ECD4")))
    pale_pink_action.triggered.connect(lambda: self.change_background_color(QColor("#F4D9D9")))
    pale_yellow_action.triggered.connect(lambda: self.change_background_color(QColor("#FDF4CD")))
    menu.addAction(pale_blue_action)
    menu.addAction(pale_green_action)
    menu.addAction(pale_pink_action)
    menu.addAction(pale_yellow_action)
    menu.exec(self.viewport().mapToGlobal(position))

def change_background_color(self, color):
    indexes = self.selectedIndexes()
    rows = set()
    for index in indexes:
        rows.add(index.row())
    for row in rows:
        for column in range(self.columnCount()):
            self.item(row, column).setBackground(color)

class MainWindow(QMainWindow): def init(self): super().init() self.table_widget = TableWidget() self.setCentralWidget(self.table_widget)

if name == "main": app = QApplication(sys.argv) qt_material.apply_stylesheet(app, theme='dark_teal.xml') # 'dark_teal.xml stylesheet = app.styleSheet() app.setStyleSheet(stylesheet + "QTableView {background-color: #514;}") window = MainWindow() window.resize(1280, 720) window.show() sys.exit(app.exec())

- data.out
```out
John 20
Mary 19
Tom 22
Jerry 21
Bob 20
Alice 20
Jack 20
kohei-noda-qcrg commented 9 months ago

@YeisonCardona Do you have an opinion on this? Since you seem to have answered questions #55 and #65, I assumed you may have a solution to this issue.

Docmorfine commented 6 months ago

Hello, i have the same problem