【问题标题】:How to assign QLinearGradient as QTableView items background color如何将 QLinearGradient 分配为 QTableView 项目背景颜色
【发布时间】:2016-09-02 20:20:48
【问题描述】:

使用 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()

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

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_()

【问题讨论】:

    标签: python qt pyqt qtableview qabstracttablemodel


    【解决方案1】:

    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_()
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2017-08-12
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2015-04-19
      • 2021-03-28
      相关资源
      最近更新 更多