简体   繁体   English

用PySide覆盖paintEvent的问题

[英]Problems overriding paintEvent with PySide

I've subclassed the QPlainTextEdit class and have tried to override the paintEvent function so that I can draw a line number area onto it. 我已经将QPlainTextEdit类子类化,并尝试覆盖paintEvent函数,以便我可以在其上绘制行号区域。

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

When the program runs I get this output: 当程序运行时,我得到这个输出:

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

My best guess is that the function hasn't been overridden properly, but I'm really not sure. 我最好的猜测是该功能没有被正确覆盖,但我真的不确定。 Can anybody tell me where I'm going wrong? 谁能告诉我哪里出错了?

You have to pass the viewport to the QPainter, same as with lists and trees. 您必须将视口传递给QPainter,与列表和树一样。

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

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

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