PDA

View Full Version : QPainter on a QtWidget?



drmacro
3rd March 2017, 16:09
I'm trying to paint on a QtWidget. In this case a slider.

I created my widget with a paint event:


class MySlider(QtWidgets.QSlider):
def __init__(self, parent=None):
super(MySlider, self).__init__(parent)

def paintEvent(self, QPaintEvent):
qp = QPainter(self)

qp.setPen(Qt.black)
size = self.size()
qp.drawLine(10,10,60,10)


And the line does show up, but the normal slider graphics (the knob and line) don't show up.

I assume I'm missing how to tell the slider widget to draw before or after my code is done. Or, am I totally off base (not unlikely for me... :p )

Mac

Added after 5 minutes:

I got it working by adding a call to super for the paint event, but is this the RIGHT way?:


class MySlider(QtWidgets.QSlider):
def __init__(self, parent=None):
super(MySlider, self).__init__(parent)

def paintEvent(self, event):
super(MySlider, self).paintEvent(event)
qp = QPainter(self)

qp.setPen(Qt.black)
size = self.size()
qp.drawLine(10,10,60,10)

anda_skoa
4th March 2017, 13:05
If you want the base class to paint something, then calling the base class implementation is the right thing to do.

Cheers,
_