在Pyqt中更改qtable中所选字符串的颜色

时间:2014-09-05 05:50:55

标签: python pyqt4

我是PyQt编程的新手,我正在使用qt4设计器设计GUI。在GUI中,我有一个表,当我按下按钮时会填充一次。 GUI显示如下图所示:

enter image description here

现在在表格中我希望“status”列中的字符串被着色,即“checked”字符串为“green”,“not_checked”字符串为“red”

我怎么能这样做,我可以使用样式表吗?请帮帮我这个

1 个答案:

答案 0 :(得分:1)

我建议使用QtGui.QItemDelegate委托展示您的数据。您也可以QTableWidgetQTableView使用。在QItemDelegate.paint (self, QPainter painter, QStyleOptionViewItem option, QModelIndex index)方法中实施。

在该方法中,如果自定义字段中的列(在本例中为第2列),请使用自定义颜色绘制它。并使用QtGui.QItemDelegate实施自定义QTableWidget已完成,放入QTableViewQAbstractItemView.setItemDelegate (self, QAbstractItemDelegate delegate)

实施例

import sys
from PyQt4 import QtGui, QtCore

class ENUM_STATUS:
    CHECKED     = QtCore.QString('checked')
    NOT_CHECKED = QtCore.QString('not_checked')

class QCustomDelegate (QtGui.QItemDelegate):
    def paint (self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 1:
            textQString = indexQModelIndex.model().data(indexQModelIndex, QtCore.Qt.EditRole).toString()
            if   textQString == ENUM_STATUS.CHECKED:
                currentQColor = QtCore.Qt.darkGreen
            elif textQString == ENUM_STATUS.NOT_CHECKED:
                currentQColor = QtCore.Qt.darkRed
            else:
                currentQColor = QtCore.Qt.darkGray
            painterQPainter.setPen(currentQColor)
            painterQPainter.drawText(optionQStyleOptionViewItem.rect, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, textQString)
        else:
            QtGui.QItemDelegate.paint(self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)

        ### <! This is initiate your QTableWidget or QTableView, Your code >! ###
        listsHorizontalHeaderItem = ['Name', 'Status']
        self.setColumnCount(len(listsHorizontalHeaderItem))
        for index in range(self.columnCount()):
            self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
        listsData = [
            ['Mr. A', ENUM_STATUS.CHECKED],
            ['Mr. B', ENUM_STATUS.NOT_CHECKED],
            ['Mr. C', ENUM_STATUS.NOT_CHECKED],
            ['Mr. D', ENUM_STATUS.CHECKED],
            ['Mr. E', ENUM_STATUS.CHECKED]]
        self.setRowCount(len(listsData))
        for row in range(len(listsData)):
            for column in range(len(listsData[row])):
                self.setItem(row, column, QtGui.QTableWidgetItem(listsData[row][column]))
        ### <! End initiate >! ###

        # After initiated, Your have to setup delegate to your QTableWidget or QTableView, Add line
        self.myQCustomDelegate = QCustomDelegate()
        self.setItemDelegate(self.myQCustomDelegate)

if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQCustomTableWidget = QCustomTableWidget()
    myQCustomTableWidget.show()
    sys.exit(myQApplication.exec_())