如何使用QSS替换表格视图的背景颜色?

时间:2014-12-19 00:05:32

标签: python qt pyqt qtableview qtstylesheets

下面的示例代码创建了一个QTableView。

enter image description here

问题:如何修改此代码,使奇数项目背景颜色为灰色,偶数项目为黑色。应该使用CSS alternate-background-color吗?那个旗帜?

import sys, os
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)        
        self.items=['One','Two','Three','Four','Five','Six','Seven']

    def rowCount(self, parent=QtCore.QModelIndex()):   
        return len(self.items)
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)):
            return QtCore.QVariant()

        item=str(self.items[index.row()])

        if role==QtCore.Qt.UserRole:
            return item
        if role==QtCore.Qt.DisplayRole:
            return item
        if role==QtCore.Qt.TextColorRole:
            return QtCore.QVariant(QtGui.QColor(QtCore.Qt.white))

    def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
        if role!=QtCore.Qt.DisplayRole:   return QtCore.QVariant()
        if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('My Column Name') 

class TableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)
        self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)        

        myModel=TableModel()
        self.setModel(myModel)      

        appStyle="""
        QTableView
        {   
            background-color: black;
            gridline-color:black;
            color: black;
            selection-color: black;
        }
        QTableView::item 
        {   
            color: white;
            background:black;            
        }
        QTableView::item:hover
        {   
            color: black;
            background:#ffaa00;            
        }
        QTableView::item:focus
        {   
            color: black;
            background:#0063cd;            
        }        
        """
        self.setStyleSheet(appStyle)

view=TableView()
view.show()   
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

以下是如何使用模型控制项目的背景颜色。 CSS稍后用于其他所有内容:

import sys, os
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)        
        self.items=['One','Two','Three','Four','Five','Six','Seven']

    def rowCount(self, parent=QtCore.QModelIndex()):   
        return len(self.items)
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)):
            return QtCore.QVariant()

        item=str(self.items[index.row()])

        if role==QtCore.Qt.UserRole:
            return item
        if role==QtCore.Qt.DisplayRole:
            return item
        if role==QtCore.Qt.TextColorRole:
            return QtCore.QVariant(QtGui.QColor(QtCore.Qt.white))
        if role == QtCore.Qt.BackgroundRole:
            if index.row()%2:
                return QtCore.QVariant(QtGui.QColor("#242424"))
            else:
                return QtCore.QVariant(QtGui.QColor(QtCore.Qt.black))

    def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
        if role!=QtCore.Qt.DisplayRole:   return QtCore.QVariant()
        if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('My Column Name') 

class TableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)
        self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)        

        myModel=TableModel()
        self.setModel(myModel)      

        appStyle="""
        QTableView
        {   
            background-color: black;
            gridline-color:grey;
            color: black;
        }
        QTableView::item 
        {   
            color: white;         
        }
        QTableView::item:hover
        {   
            color: black;
            background: #ffaa00;            
        }
        QTableView::item:focus
        {   
            color: black;
            background: #0063cd;            
        }        
        """
        self.setStyleSheet(appStyle)

view=TableView()
view.show()   
sys.exit(app.exec_())