[PyKDE] calling a paintEvent in a custom control due to a mouse event

Bernhard VonZastrow bvz at c381.com
Wed Sep 27 23:27:07 BST 2006


Hello all, I am very new to PyQt so hopefully this isn't a completely  
boneheaded question...

I am writing my own disclosureTriangle widget using PyQt4.  I  
subclassed the QWidget object and implemented my own paintEvent  
function.  In this function I am drawing the control using an  
instance of a QPainter object.  (I got the basics for how to do this  
from a website who's url I can no longer find).

The code to draw this control is as follows:

     #----------------------------------------------
     def paintEvent(self, event):

         if not self.isVisible():
             return

         if self.isEnabled():
             color = self.normalColor
         else:
             color = self.disabledColor

         if self.mouseDown == True:
             color = self.blackColor

         rightPolygon = QPolygon(3)
         rightPolygon.setPoint(0,1,1)
         rightPolygon.setPoint(1, 9, 6)
         rightPolygon.setPoint(2, 1, 11)

         downPolygon = QPolygon(3)
         downPolygon.setPoint(0,1,2)
         downPolygon.setPoint(1, 11,)
         downPolygon.setPoint(2, 6, 10)

         if self.state == "expanded":
             self.drawImage(color, downPolygon)
         else:
             self.drawImage(color, rightPolygon)

     #----------------------------------------------
     def drawImage(self, color, polygon):
         painter = QPainter()
         painter.begin(self)
         painter.setRenderHint(QPainter.Antialiasing, True)
         painter.setBrush(color)
         painter.setPen(color)
         painter.drawPolygon(polygon, Qt.OddEvenFill)
         painter.end()


This all works as expected.  The question is how do I tell the  
paintEvent to fire from within my code?  I am implementing my own  
mousePressEvent code.  When the user has pressed the mouse, I want to  
darken the image.  How do I tell my widget (from within itself) that  
it needs to repaint itself?

I've tried the following:

     #----------------------------------------------
     def mousePressEvent(self, event):
         if event.button() == Qt.LeftButton:
             self.mouseDown = True
             print "asdf"
             newRect = QRect(0,0,100,100)
             newEvent = QPaintEvent(newRect)
             self.paintEvent(newEvent)


but I get the following output:

asdf
QPainter::begin: Widget painting can only begin as a result of a  
paintEvent
Painter must be active to set rendering hints
QPainter::end: Painter is not active, aborted


Any ideas how to go about implementing this?  Any help at all, even a  
push in the right direction would be appreciated.

Thanks
Ben




More information about the PyQt mailing list