2015-01-31 74 views
0

什麼是一個正確的CSS語法來設計QTableView項目checkboxes?由於QCheckBox是QTableView中的一部分,它是棘手的與CSS的複選框...如何樣式QTableView CSS

enter image description here

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_()) 
+0

使用'QTableView中QCheckBox ::指標:檢查{/*...*/}'選擇。請注意,這隻適用於您有真正的複選框小部件。我建議你爲此使用代表。 – 2015-02-01 09:40:41

+0

我在sourceModel的data()方法中使用'if role == Qt.CheckStateRole:return QVariant(Qt.Checked)'作爲結果使用複選框。讓我看看,如果這仍然會工作......感謝一個主意! – alphanumeric 2015-02-01 23:17:16

+0

不幸的是'QTableView QCheckBox :: indicator:checked {...}'不起作用.... – alphanumeric 2015-02-01 23:26:18

回答

1

我想象它的一個類似的部門對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; 
}