You must set the background color of the element. There are several ways to do this (full script further):
- Option 1: set the background in the element, then add the element to the table.
item1 "row1" . , /.
item1 = QtGui.QTableWidgetItem('row1')
if row % 2 == 0:
item1.setBackground(QtGui.QColor(255, 128, 128))
self.table.setItem(row,0,item1)
- : 1, 0:
self.table.item(1,0).setBackground(QtGui.QColor(125,125,125))
script, , :
from PyQt4 import QtCore
from PyQt4 import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.table = QtGui.QTableWidget()
self.table.setColumnCount(2)
self.setCentralWidget(self.table)
data1 = ['row1','row2','row3','row4']
data2 = ['1','2.0','3.00000001','3.9999999']
self.table.setRowCount(4)
for row in range(4):
item1 = QtGui.QTableWidgetItem(data1[row])
if row % 2 == 0:
item1.setBackground(QtGui.QColor(255, 128, 128))
self.table.setItem(row,0,item1)
self.table.item(1,0).setBackground(QtGui.QColor(125,125,125))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
:
