【发布时间】:2015-01-31 19:29:28
【问题描述】:
设置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_())
【问题讨论】:
-
使用
QTableView QCheckBox::indicator:checked{ /*...*/ }选择器。请注意,这仅适用于您拥有真正的复选框小部件。我建议你为此使用代表。 -
我正在使用复选框作为在 sourceModel 的
data()方法中使用if role == Qt.CheckStateRole: return QVariant(Qt.Checked)的结果。让我看看这是否仍然有效......谢谢你的想法! -
很遗憾
QTableView QCheckBox::indicator:checked{...)不起作用....
标签: python css qt pyqt qtableview