Delegate to paint items in combo popup:
class LineStyleDelegate(QtGui.QItemDelegate):
def __init__(self, object, parent = None):
QtGui.QItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
data = index.model().data(index, QtCore.Qt.UserRole)
if data.isValid() and data.toPyObject() is not None:
data = data.toPyObject()
painter.save()
rect = option.rect
rect.adjust(+5, 0, -5, 0)
pen = QtGui.QPen()
pen.setColor(QtCore.Qt.black)
pen.setWidth(3)
pen.setStyle(data)
painter.setPen(pen)
middle = (rect.bottom() + rect.top()) / 2
painter.drawLine(rect.left(), middle, rect.right(), middle)
painter.restore()
else:
QtGui.QItemDelegate.paint(self, painter, option, index)
painter.drawLine(rect.left(), middle, rect.right(), middle)
painter.restore()
else:
QtGui.QItemDelegate.paint(self, painter, option, index)
paintEvent to paint current item in combo. You can, of course, paint it manually, but there is the simple way to draw combo box control itself (if you want an arrowbutton or smth in the current):
def paintEvent(self, e):
data = self.itemData(self.currentIndex(), QtCore.Qt.UserRole)
if data.isValid() and data.toPyObject() is not None:
data = data.toPyObject()
p = QtGui.QStylePainter(self)
p.setPen(self.palette().color(QtGui.QPalette.Text))
opt = QtGui.QStyleOptionComboBox()
self.initStyleOption(opt)
p.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt)
painter = QtGui.QPainter(self)
painter.save()
rect = p.style().subElementRect(QtGui.QStyle.SE_ComboBoxFocusRect, opt, self)
rect.adjust(+5, 0, -5, 0)
pen = QtGui.QPen()
pen.setColor(QtCore.Qt.black)
pen.setWidth(3)
pen.setStyle(data)
painter.setPen(pen)
middle = (rect.bottom() + rect.top()) / 2
painter.drawLine(rect.left(), middle, rect.right(), middle)
painter.restore()
else:
QtGui.QComboBox.paintEvent(self, e)