如何设置 QTableView 的样式 CSS

How to style QTableView CSS

设置 QTableView 项目 checkboxes 样式的正确 CSS 语法是什么?由于 QCheckBox 是 QTableView 的一部分,因此使用 CSS...

进入复选框很棘手

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

appStyle="""

QTableView
{
    alternate-background-color: #1F1F1F;
    background-color: gray;
    gridline-color: gray;
    color: gray;
}
QTableView::item 
{   
    color: white;        
}

QTableView::item:focus
{   
    color: gray;
    background: #0063cd;            
}        
QTableView::item:selected
{   
    color: gray;
    background: #0063cd;            
}

QCheckBox::indicator:checked, QCheckBox::indicator:unchecked{
    color: #b1b1b1;
    background-color: #323232;
    border: 1px solid #b1b1b1;
    border-radius: 1px;
    width: 7px;
    height: 7px;
    margin: 0px 5px 0 5px;
}

"""
class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_001','Item_002','Item_003']
    def rowCount(self, parent=QModelIndex()):
        return len(self.items)      
    def columnCount(self, parent=QModelIndex()):
        return 1
    def flags (self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable
    def data(self, index, role):
        if not index.isValid(): return QVariant()
        if role==Qt.DisplayRole:    return QVariant(self.items[index.row()])             
        elif role == Qt.CheckStateRole:   return QVariant(Qt.Checked)
        else:            return QVariant()

class MyWindow(QTableView):
    def __init__(self, *args):        
        QTableView.__init__(self, *args)
        tableModel=Model(self)
        self.setModel(tableModel)
        self.setStyleSheet(appStyle)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

我想它是一个与 QCheckBox::indicatorQListView::indicatorQTreeView::indicator 相似的扇区...尝试:

QTableView::indicator:checked {
    color: #b1b1b1;
    background-color: #323232;
    border: 1px solid #b1b1b1;
    border-radius: 1px;
    width: 7px;
    height: 7px;
    margin: 0px 5px 0 5px;
}