簡體   English   中英

如何設置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;
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM