title: PyQt5 Change BackgroundRole Settings in QAbstractTableModel
body: <p>I am using a QTableView in PyQT5 to display a table of data.</p>
  
  <p>I can set the background colour of each cell based on a condition. For example to turn the cell red:</p>
  
  <pre><code>class TableModel(QtCore.QAbstractTableModel):
  
      def __init__(self, data):
          super(TableModel, self).__init__()
          self._data = data
  
      def data(self, index, role):
  
          if role == Qt.BackgroundRole:
              if condition == ...
                  return QtGui.QColor('#9C0006')
  
  </code></pre>
  
  <p>However instead of applying the background colour to the whole cell, I want to apply the colour to only part of the cell (based on the cell's value). For example like this:</p>
  
  <p><a href="https://i.stack.imgur.com/FKfOC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FKfOC.png" alt="enter image description here"></a></p>
  
  <p>Is this possible with QT, and if so, what settings would I need to change? I believe <code>QBrush</code> allows me set the fill pattern (i.e. a gradient like the image above), however is there class which handles the <strong>size</strong> of the background colour 'item' relative to the size of the cell, and it's placement within the cell?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I now have the following code in Python, which sets the length of the gradient equal to 50% of cell width (which I can later set dynamically).</p>
  
  <pre><code>if role == Qt.BackgroundRole:
      percent = 0.5
  
      gradient = QtGui.QLinearGradient(QtCore.QPointF(0, 0), QtCore.QPointF(1, 0))
      gradient.setColorAt(0, Qt.red)
      gradient.setColorAt(percent, Qt.white)
      gradient.setColorAt(1, Qt.white)
      gradient.setCoordinateMode(QtGui.QLinearGradient.ObjectMode)
  
  
      return QtGui.QBrush(gradient)
  </code></pre>
  
  <p>This provides the following formatting:</p>
  
  <p><a href="https://i.stack.imgur.com/Jl9bL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jl9bL.png" alt="enter image description here"></a></p>
  
  <p>Is there a way to provide a padding of say 5% to the top and bottom of each cells, so that the gradient formatting only fills 90% of the cell's vertical space rather than 100% currently? I.e. something like these (see top 3 rows).</p>
  
  <p><a href="https://i.stack.imgur.com/rEFrK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rEFrK.png" alt="enter image description here"></a></p>
  
  <p>I imagine I need to set the QBrush cooridinates so that the QBrush operation only takes place on part of the cell? However I can't seem to find anything within the QBrush documentation related to coordinates. Is this possible?</p>