python - 如何将 QLinearGradient 指定为 QTableView 项目背景颜色

标签 python qt pyqt qtableview qabstracttablemodel

使用 QLineEdit 的调色板,我们可以指定 QGradient 作为它的背景色:

line = QtGui.QLineEdit()
palette = line.palette()
QRectF = QtCore.QRectF(line.rect())
gradient = QtGui.QLinearGradient(QRectF.topLeft(), QRectF.topRight())
palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(gradient))
line.setPalette(palette)
line.show()

enter image description here

在使用QTableView 及其QAbstractTableModel 时,我从模型的data 方法中为每个BackgroundColorRole 请求。我宁愿将渐变分配给 tableView“项目”,而不是纯色。 如何分配渐变而不是纯色?

enter image description here

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Model(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 3 
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 3

    def data(self, index, role):
        if not index.isValid(): return 

        if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
            return self.items[index.row()][index.column()]

        if role == QtCore.Qt.ForegroundRole:
            return QtGui.QColor("white")

        if role == QtCore.Qt.BackgroundColorRole:
            return QtGui.QColor("gray")

def onClick(index):
    print 'clicked index:  %s'%index

tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.clicked.connect(onClick)

tableView.show()
app.exec_()

最佳答案

BackgroundRole 用于生成一个QBrush,它可以有渐变。请参见下面的示例。 BackgroundColorRole 似乎已过时,因此最好使用 BackgroundRole,即使您不想要渐变也是如此。

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

def create_gradient_brush():
    horGradient = QtGui.QLinearGradient(0, 0, 100, 0)
    verGradient = QtGui.QLinearGradient(0, 0, 0, 20)
    gradient = verGradient 
    gradient.setColorAt(0.0, QtGui.QColor("blue"))
    gradient.setColorAt(1.0, QtGui.QColor("red"))
    brush = QtGui.QBrush(gradient)
    return brush


class Model(QtCore.QAbstractTableModel):

    # The cell size is most likely unavailable in the model, it could be 
    # different per view, so we make a cell size-independent gradient.
    BG_BRUSH = create_gradient_brush()

    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 3 
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 3

    def data(self, index, role):
        if not index.isValid(): return 

        if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
            return self.items[index.row()][index.column()]

        if role == QtCore.Qt.ForegroundRole:
            return QtGui.QColor("white")

        # BackgroundColorRole is obsolete, use BackgroundRole, 
        # which returns a QBrush.
        if role == QtCore.Qt.BackgroundRole:
            return self.BG_BRUSH


def onClick(index):
    print 'clicked index:  %s'%index

tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.clicked.connect(onClick)

tableView.show()
app.exec_()

关于python - 如何将 QLinearGradient 指定为 QTableView 项目背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37094753/

相关文章:

python - 从多季度期间提取季度数据

python - 收到 `KeyError: u' 没有名为 XYZ'` 的项目错误

c++ - QDialog不在 parent 的中心

python - 将 C++ 类移植到 PyQt

python - Raspberry Pi python显示内存中的图像

python - 将多索引中的条件列与同一索引对齐时出现问题

css - 如何在 QFileDialog 上获得大图标?

python - QDialog - 防止在 Python 和 PyQt 中关闭

python - 如何保存各个值并重新打开它们以供以后使用?

python - 如何获取 QComboBox 的当前文本内容?