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()
上一篇:python-如何在基于事件的QTreeView中启用/禁用项目?


下一篇:Oracle 18c 数据库中scott用户不存在的解决方法