我需要幫助爲我的QTableView着色。 @rainer幫助我在初始化表格時添加顏色,但是現在,我已經有了一個帶有數據的表格(但沒有顏色//我的數據是在我的表格中打開了一個csv),並且我想創建一個按鈕,當點擊它時在某些行中對tableview進行着色,比如當有一行有-2(data)時,它將會是帶有藍色的顏色.. - 我有一個按鈕和一個表格。該按鈕將csv數據加載到我的tableview中。我想要一個新的按鈕,使這個表的行變成彩色。 (但只有顏色有數據-2,例如行) 有些代碼:pyqt - 用現有數據在QTableView中放置顏色
self.fileName = (_fromUtf8('tweets.csv'))
self.tableView = QTableView(self.tabSentimento)
self.tableView.setGeometry(QRect(550,10,510,700))
self.tableView.setObjectName(_fromUtf8("TabelaSentimento"))
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.pushButtonLoad = QPushButton(self.tabSentimento)
self.pushButtonLoad.setGeometry(QRect(550,720,130,30))
self.pushButtonLoad.setObjectName(_fromUtf8("buttonLoadCSV"))
self.pushButtonLoad.setText(QApplication.translate("Form", "Process!", None, QApplication.UnicodeUTF8))
self.pushButtonLoad.setStyleSheet('color:red;background-color:rgb(255, 255, 153);border:1px solid purple;')
self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)
def loadCsv(self, fileName):
with open(fileName, "rb") as fileInput:
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
self.model.appendRow(items)
def on_pushButtonLoad_clicked(self):
print self.fileName
self.loadCsv(self.fileName)
'QStandardItem'有一個方法'的setBackground()'你可以用它來設置它的背景色:http://qt-project.org/doc/qt-4.8/qstandarditem.html#setBackground。所以,當你點擊第二個按鈕時,只需遍歷所有行並相應地設置顏色。 – rainer 2013-03-27 20:01:23