我正在尝试创建一个具有分隔线的 TextEdit 小部件。首先,我创建了一个MyTextEdit
类(作为 a 的子类QTextEdit
)并覆盖了它的paintEvent()
方法:
import sys
from PyQt4.QtGui import QApplication, QTextEdit, QPainter
class MyTextEdit(QTextEdit):
"""A TextEdit widget derived from QTextEdit and implementing its
own paintEvent"""
def paintEvent(self, event):
painter = QPainter(self)
painter.drawLine(0, 10, 10, 10)
QTextEdit.paintEvent(self, event)
app = QApplication(sys.argv)
textEdit = MyTextEdit()
textEdit.show()
sys.exit(app.exec_())
尝试执行此代码时,我收到很多以下错误:
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::begin: Widget painting can only begin as a result of a paintEvent
...
我究竟做错了什么?