我已经将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
我最好的猜测是该功能没有被正确覆盖,但我真的不确定。谁能告诉我哪里出错?
答案 0 :(得分:4)
您必须将视口传递给QPainter,与列表和树一样。
def paintEvent(self, e):
super(CodeEditor, self).paintEvent(e)
qp = QtGui.QPainter()
qp.begin(self.viewport())
self.drawLineNoArea(qp)
qp.end()