【问题标题】:Selection highlight in PyQt4 QTableWidget fill selected cell's background with full block colorPyQt4 QTableWidget中的选择突出显示用全块颜色填充选定单元格的背景
【发布时间】:2023-03-24 06:30:01
【问题描述】:

我正在开发一个小型 PyQt4 任务管理器。一个类似的问题在这里问Change QTableWidget default selection color, and make it semi transparent。在这篇文章中,我尝试使用 setStyleSheet 来选择背景颜色的不透明度,但突出显示仍然会覆盖单元格背景颜色。有人可以帮我展示如何将其更改为边框颜色吗?

下图是我目前的结果

这是我愿意实现的,如您所见,高亮选择只是覆盖背景颜色,而不是覆盖它。

最后,希望我的问题对大家来说足够清楚,如果发现任何不清楚或错误的地方,请告诉我,我会尽快修复! 谢谢!

【问题讨论】:

  • 我建议改进你的问题,链接是参考和次要的,描述你想要什么并放置你尝试过的东西。
  • 我的问题更新了,谢谢你的建议!
  • 你按我的答案工作了吗?
  • 您好 [![name][1]][1] ,感谢您的回复。我尝试运行脚本,但不确定为什么会出现此错误。 (第 25 行,如果 option.widget: AttributeError: 'QStyleOptionViewItem' object has no attribute 'widget')我唯一添加的是导入模块。 from PyQt4.QtGui import * from PyQt4.QtCore import Qt import sys [1]: stackoverflow.com/users/6622587/eyllanesc
  • 我认为是pyqt4版本的问题,我已经更新了代码,请重新检查

标签: python python-2.7 pyqt pyqt4 qtablewidget


【解决方案1】:

更改颜色的一种方法是使用委托。

为此,我们必须获取当前的背景颜色,获取背景颜色的任务很繁琐,因为 QTableWidget 有自己的颜色作为背景,它也有你添加到 QTableWidgets 和其他类型元素的颜色,所以我answer 目前支持有限,但这个想法是可扩展的。

作为被选元素背景显示的颜色是背景颜色和正确选择的颜色的平均值,本例中我们选择颜色#cbedff

我已经在以下课程中实现了上述所有内容:

class TableWidget(QTableWidget):
    def __init__(self, *args, **kwargs):
        QTableWidget.__init__(self, *args, **kwargs)

        class StyleDelegateForQTableWidget(QStyledItemDelegate):
            color_default = QColor("#aaedff")

            def paint(self, painter, option, index):
                if option.state & QStyle.State_Selected:
                    option.palette.setColor(QPalette.HighlightedText, Qt.black)
                    color = self.combineColors(self.color_default, self.background(option, index))
                    option.palette.setColor(QPalette.Highlight, color)
                QStyledItemDelegate.paint(self, painter, option, index)

            def background(self, option, index):
                item = self.parent().itemFromIndex(index)
                if item:
                    if item.background() != QBrush():
                        return item.background().color()
                if self.parent().alternatingRowColors():
                    if index.row() % 2 == 1:
                        return option.palette.color(QPalette.AlternateBase)
                return option.palette.color(QPalette.Base)

            @staticmethod
            def combineColors(c1, c2):
                c3 = QColor()
                c3.setRed((c1.red() + c2.red()) / 2)
                c3.setGreen((c1.green() + c2.green()) / 2)
                c3.setBlue((c1.blue() + c2.blue()) / 2)

                return c3

        self.setItemDelegate(StyleDelegateForQTableWidget(self))

例子:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = TableWidget()
    w.setColumnCount(10)
    w.setRowCount(10)
    for i in range(w.rowCount()):
        for j in range(w.columnCount()):
            w.setItem(i, j, QTableWidgetItem("{}".format(i * j)))
            if i < 8 and j < 8:
                color = QColor(qrand() % 256, qrand() % 256, qrand() % 256)
                w.item(i, j).setBackground(color)
    w.show()
    sys.exit(app.exec_())

取消选择:

已选择:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多