2012-09-01 28 views
5

Estoy tratando de crear un widget TextEdit que tendrá una línea de delimitador. Como punto de partida, he creado una clase MyTextEdit (como una subclase de un QTextEdit) y anulado su paintEvent() método:Reemplazando QPaintEvents en PyQt

import sys 
from PyQt4.QtGui import QApplication, QTextEdit, QPainter 

class MyTextEdit(QTextEdit): 
    """A TextEdit widget derived from QTextEdit and implementing its 
     own paintEvent""" 

    def paintEvent(self, event): 
     painter = QPainter(self) 
     painter.drawLine(0, 10, 10, 10) 
     QTextEdit.paintEvent(self, event) 

app = QApplication(sys.argv) 
textEdit = MyTextEdit() 
textEdit.show() 

sys.exit(app.exec_()) 

intentar ejecutar este código, consigo un montón de los siguientes errores:

QPainter::begin: Widget painting can only begin as a result of a paintEvent 
QPainter::begin: Widget painting can only begin as a result of a paintEvent 
... 

¿Qué estoy haciendo mal?

Respuesta

7

Si un widget tiene una viewport, usted tiene que pasar eso a la QPainter constructor:

painter = QPainter(self.viewport()) 
+0

interesante, sí, no podía entender por qué no estaba funcionando. Esa ventana siempre me fastidia. ¡Gracias! –