编程语言
首页 > 编程语言> > python-用PySide覆盖paintEvent的问题

python-用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

我最好的猜测是该函数尚未正确重写,但是我不确定.谁能告诉我我要去哪里错了?

解决方法:

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

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

标签:pyside,override,paintevent,python
来源: https://codeday.me/bug/20191101/1985643.html