Query on Table View - Model/View programming
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.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
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 "data:" , index.row() , index.column() , value
#print value
value = QColor(value)
#print value
pixmap = QPixmap(26,26)
pixmap.fill(value)
icon = QIcon(pixmap)
return icon
class MainWindow(QMainWindow):
def __init__( self , parent = None ):
super( MainWindow , self ).__init__(parent)
self.tv = QTableView(self)
self.dm = paletteTableview( data( 4 , 5 ) )
self.tv.setModel( self.dm )
self.setCentralWidget(self.tv)
def data(row,column):
import random
test_data = []
temp_data= []
for i in range(row):
for j in range(column):
temp_data.append( "#%06X" % random.randint( 0 , 0xFFFFFF ) )
test_data.append(temp_data)
temp_data = []
return test_data
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == '__main__':
main()
精彩评论