【问题标题】:How to set each item's selection color of QTableWidget in PyQt5PyQt5中如何设置QTableWidget每一项的选择颜色
【发布时间】:2018-06-01 12:15:05
【问题描述】:

我希望在选择项目时有不同的选择颜色。但QTableWidget::item:selected{ background-color: } 仅在仅选择一项时有效,否则所有选定项将具有相同的选择颜色。那么有没有办法让每个项目都有单独的选择颜色?

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.MainWindow=MainWindow
        self.MainWindow.resize(300, 100)
        self.centralwidget = QtWidgets.QWidget(self.MainWindow)
        self.MainWindow.setCentralWidget(self.centralwidget)
        """table """
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.insertRow(0)
        self.tableWidget.setColumnCount(2)
        
        self.tableWidget.setItem(0,0,QtWidgets.QTableWidgetItem("red"))
        self.tableWidget.setItem(0,1,QtWidgets.QTableWidgetItem("blue"))
        self.tableWidget.itemSelectionChanged.connect(self.ChangeSelectionColor)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def ChangeSelectionColor(self):
        try:
            for item in self.tableWidget.selectedItems():
                col=item.column()
            self.tableWidget.setStyleSheet("QTableWidget::item:selected{ background-color: %s }"%color_list[col])
        except UnboundLocalError:
            pass
if __name__ == "__main__":
    import sys
    color_list=['red','blue']
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

一个选择效果很好。

多选只是将颜色应用于所有选定的项目。我希望左边的一旦被选中就变成红色。

【问题讨论】:

  • 颜色取决于什么?
  • @eyllanesc 在main 中定义的color_list。当column=i时,它的选择颜色应该是color_list[i]
  • 我想你还没有理解我的问题,我说你有一个2行3列的表格,颜色是什么,每个单元格被选中时应该有什么?
  • @eyllanesc 我发布的问题只是一个简化。在我的项目中,每个项目的颜色也都存储在一个列表中。
  • 我想给出一个可靠的答案,假设列表长度为 5 并且选择了 6 项,第六项应该是什么颜色?

标签: python qt pyqt pyqt5 qtablewidget


【解决方案1】:

在这种情况下使用 qss 是不合适的,因为它们有很多限制,适合实现一个委托,在这种情况下是一个继承自 QStyledItemDelegate 的类。但在此之前我们必须通过QTableWidgetItem的setData方法保存颜色信息:

it = QTableWidgetItem("some_text")
it.setData(Qt.UserRole, some_color)

然后QStyledItemDelegate的paint方法被覆盖,选择颜色改变:

class ColorDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        color = index.data(Qt.UserRole)
        option.palette.setColor(QPalette.Highlight, color)
        QStyledItemDelegate.paint(self, painter, option, index)

那么委托就成立了:

your_qtablewidget.setItemDelegate(ColorDelegate())

下面是一个完整的例子:

from PyQt5.QtWidgets import QApplication, QStyledItemDelegate, QTableWidget, QTableWidgetItem, QStyle
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import qrand, Qt

class ColorDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        color = index.data(Qt.UserRole)
        option.palette.setColor(QPalette.Highlight, color)
        QStyledItemDelegate.paint(self, painter, option, index)


def fun(n_rows, n_columns):
    return [[QColor(qrand() % 256, qrand() % 256, qrand() % 256) for i in range(n_rows)] for j in range(n_columns)]

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    n_rows, n_columns = 10, 10
    colors = fun(n_rows, n_columns)
    w = QTableWidget()
    w.setColumnCount(n_columns)
    w.setRowCount(n_columns)
    for i in range(w.rowCount()):
        for j in range(w.columnCount()):
            it = QTableWidgetItem("{}-{}".format(i, j))
            it.setData(Qt.UserRole, colors[i][j])
            w.setItem(i, j, it)
    w.setItemDelegate(ColorDelegate())
    w.show()
    sys.exit(app.exec_())

【讨论】:

  • 谢谢。你是最棒的!
  • 我仍然有一个困惑。选择后字体颜色会自动变为白色。如何避免这种情况并保持字体颜色不变?
  • 您将添加以下行: option.palette.setColor(QPalette.HighlightedText, color) 制作颜色,此时您想要的任何文本颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
相关资源
最近更新 更多