Question

The longer I stare at the code, the less I understand it, but the funnier it gets. In one case QPainter draws the circles, in another it does not.

My problem is that if it is successful, QPainter does not draw. If, on the other hand, it was not successful, it does draw. What I actually want to achieve is changing the color depending on the state. This rather ugly code is for illustration. Moving the QP.begin() outside the loop or having just a single drawEllipse() in the end outside the if-clause does not do any difference.

def paintEvent(self, e):
        # compute here diameter, offsets, etc. but nothing qt related
        for i in xrange(5):
            painter = QtGui.QPainter(self)
            x_offset += 5
            if self.isSuccessful():
                print "Successful"
                painter.setBrush(QtGui.QColor(0,255,0))
                painter.drawEllipse(x_offset, y_offset, diameter, diameter)
            else:
                print "Not yet"
                painter.setBrush(QtGui.QColor(0,0,255))
                painter.drawEllipse(x_offset, y_offset, diameter, diameter)
            painter.end()

Can someone tell me why in one case the ellipse is actually painted and in the other it is not?

The output is:

...
Not yet
Not yet
Not yet
Not yet
Not yet
Successfull
Successfull
Successfull
Successfull
Successfull
...

It does not matter whether the ellipse has been drawn before or not. In case I comment the drawEllipse() in the "not yet"-case, no circle is drawn.

Was it helpful?

Solution

The problem was the source of the paintEvent I created. The paintEvent was called when a containing textbox got focus. But the update region for the event only included the text box, which actually does make sense but I did not know. So paintEvent was called, but the update region did not include the circles.

I circumvented the problem now by calling the update-method for the whole widget, which solves the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top