简体   繁体   English

PyQT:傻QPainter.drawEllipse

[英]PyQT: Silly QPainter.drawEllipse

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. 在一种情况下,QPainter绘制了圆圈,而在另一种情况下,则没有绘制。

My problem is that if it is successful, QPainter does not draw. 我的问题是,如果成功,QPainter 不会绘制。 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. 将QP.begin()移到循环外,或者如果if子句末尾只有一个drawEllipse(),则没有任何区别。

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. 如果我在“ not yet”情况下对drawEllipse()进行注释,则不会绘制任何圆圈。

The problem was the source of the paintEvent I created. 问题是我创建的paintEvent的来源。 The paintEvent was called when a containing textbox got focus. 当包含文本框获得焦点时,将调用paintEvent。 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. 因此调用paintEvent,但更新区域不包括圆圈。

I circumvented the problem now by calling the update-method for the whole widget, which solves the problem. 我现在通过调用整个小部件的update-method来解决该问题,从而解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM