用PySide覆盖paintEvent的问题

时间:2012-02-09 22:49:30

标签: python override pyside paintevent

我已经将QPlainTextEdit类子类化,并尝试覆盖paintEvent函数,以便我可以在其上绘制行号区域。

def paintEvent(self, e):
    super(CodeEditor, self).paintEvent(e)
    qp = QtGui.QPainter()
    qp.begin(self)
    self.drawLineNoArea(qp)
    qp.end()

当程序运行时,我得到这个输出:

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted

我最好的猜测是该功能没有被正确覆盖,但我真的不确定。谁能告诉我哪里出错?

1 个答案:

答案 0 :(得分:4)

您必须将视口传递给QPainter,与列表和树一样。

def paintEvent(self, e):
    super(CodeEditor, self).paintEvent(e)
    qp = QtGui.QPainter()
    qp.begin(self.viewport())
    self.drawLineNoArea(qp)
    qp.end()