alphanumeric
alphanumeric

Reputation: 19379

How to assign QLinearGradient as QTableView items background color

Using QLineEdit's palette we can assign QGradient as its background color:

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

While working with QTableView and its QAbstractTableModel I return a solid QColor from model's data method for every BackgroundColorRole request. Instead of a solid color I would rather assign a gradient to tableView "item". How to assign a gradient instead of solid color?

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

Upvotes: 2

Views: 2563

Answers (1)

titusjan
titusjan

Reputation: 5546

The BackgroundRole is used to generate a QBrush, which can have a gradient. See the example below. The BackgroundColorRole appears to be obsolete so it's probably better to use the BackgroundRole even if you don't want a gradient.

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

Upvotes: 2

Related Questions