QTableWidget设置标题Cell的背景

时间:2015-10-12 18:47:32

标签: qt python-2.7 pyqt qtstylesheets

我知道如何设置水平标题背景颜色,但如何设置指定行的标题背景颜色?在这个例子中,假设我想让标签行“3”为红色?

enter image description here

上面的截图代码:

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *



class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()


    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.rigTable = QTableWidget(1, 3, self)
        style = "::section {""background-color: lightblue; }"
        self.rigTable.horizontalHeader().setStyleSheet(style)
        self.rigTable.setShowGrid(False)

        self.rigTable.setCellWidget(0, 0, QLabel("A"))
        self.rigTable.setCellWidget(0, 1,  QLabel("B"))      

        self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

您可以使用QTableWidgetItem类:

header = QtGui.QTableWidgetItem("3")
header.setBackground(QtGui.QColor("red"))
self.rigTable.setHorizontalHeaderItem(2, header)