import sys
from PyQt4 import QtGui
class MyStyleDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent=None):
super(MyStyleDelegate, self).__init__(parent)
def paint(self, painter, option, index):
super(MyStyleDelegate, self).paint(painter, option, index)
modelIndex = index
model = index.model()
modelIndex = model.mapToSource(index)
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
style
= options.
widget.
style() if options.
widget else QtGui.
QApplication.
style() style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
def main():
for r in range(5):
for c in range(5):
model.setItem(r, c, item)
proxy.setSourceModel(model)
w.setModel(proxy)
w.setSortingEnabled(True)
w.show()
delegate = MyStyleDelegate()
w.setItemDelegate(delegate)
w.setStyleSheet('''
QWidget::item:hover {
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #000000);
color: #000000; }
''')
sys.exit(app.exec_())
if __name__ == '__main__':
main()
import sys
from PyQt4 import QtGui
class MyStyleDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent=None):
super(MyStyleDelegate, self).__init__(parent)
def paint(self, painter, option, index):
super(MyStyleDelegate, self).paint(painter, option, index)
modelIndex = index
model = index.model()
if isinstance(model, QtGui.QAbstractProxyModel):
modelIndex = model.mapToSource(index)
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
style = options.widget.style() if options.widget else QtGui.QApplication.style()
style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
def main():
app = QtGui.QApplication(sys.argv)
model = QtGui.QStandardItemModel(5, 5)
for r in range(5):
for c in range(5):
item = QtGui.QStandardItem("R%s C%s" % (r ,c))
model.setItem(r, c, item)
proxy = QtGui.QSortFilterProxyModel()
proxy.setSourceModel(model)
w = QtGui.QTableView()
w.setModel(proxy)
w.setSortingEnabled(True)
w.show()
delegate = MyStyleDelegate()
w.setItemDelegate(delegate)
w.setStyleSheet('''
QWidget::item:hover {
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #000000);
color: #000000; }
''')
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To copy to clipboard, switch view to plain text mode
You can remove lines 9 to 15 and 20 with the same result
Bookmarks