Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kohei-noda-qcrg opened this issue May 31, 2023 · 2 comments

Comments

@kohei-noda-qcrg
Copy link

kohei-noda-qcrg commented May 31, 2023

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?

#55 (comment) (add custom-stylesheets) did not work in my case.

  • Without qt_material
without-qt_material.mp4
  • With qt_material
use-qt_material.mp4
  • Source code
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor, QAction, QPalette, QDragEnterEvent
from PySide6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QMenu, QFileDialog, QMessageBox, QInputDialog
import qt_material

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
John 20
Mary 19
Tom 22
Jerry 21
Bob 20
Alice 20
Jack 20
@kohei-noda-qcrg
Copy link
Author

@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
Copy link

Hello, i have the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants