I’m practicing some examples on model-view programming and have a query why the colors are not correctly represented.
The code is as follows:
I have created a Table View
table_view = QTableView()
table_view.show()
table_view.setModel(model)
test_data = data(4,5)
model = paletteTableview(test_data)
class paletteTableview(QAbstractTableModel):
def __init__(self, test_data, parent=None):
super(paletteTableview, self).__init__(parent)
self.__test_data = test_data
def rowCount(self, parent):
return len(self.__test_data)
def columnCount(self, parent):
return len(self.__test_data[0])
def data(self,index,role):
if role == Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.__test_data[row][column]
return value
if role == Qt.DecorationRole:
row = index.row()
column = index.column()
value = self.__test_data[row][column]
#print value
value = QColor(value)
#print value
pixmap = QPixmap(26,26)
pixmap.fill(value)
icon = QIcon(pixmap)
return icon
def data(row,column):
test_data = []
k = "FF0000"
temp_data= []
for i in range(row):
for j in range(column):
list = ['#']
for a in k:
list.append(a)
d = ''.join(list)
temp_data.append(d)
c = int(k,16)
c = c + 1
k = "%X" % c
test_data.append(temp_data)
temp_data = []
return test_data
Am I missing something here?? please let me know.
I test your code and there is no problem but the colors is nearly the same on my display. I think you may meet the same problem, which seems all the color is red without any clear difference. I suggest to use another way to generate the color data ( return by “data” function ).
I give my whole test code, you can run and try to found some useful thing for you.
It can run OK using pyqt 4.8.3.1 for python2.7 under my win7 system.