0
什么是一个正确的CSS语法来设计QTableView
项目checkboxes
?由于QCheckBox是QTableView中的一部分,它是棘手的与CSS的复选框...如何样式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_())
使用'QTableView中QCheckBox ::指标:检查{/*...*/}'选择。请注意,这只适用于您有真正的复选框小部件。我建议你为此使用代表。 – 2015-02-01 09:40:41
我在sourceModel的data()方法中使用'if role == Qt.CheckStateRole:return QVariant(Qt.Checked)'作为结果使用复选框。让我看看,如果这仍然会工作......感谢一个主意! – alphanumeric 2015-02-01 23:17:16
不幸的是'QTableView QCheckBox :: indicator:checked {...}'不起作用.... – alphanumeric 2015-02-01 23:26:18