你应该为重画的控件写一个新类,继承自 QWidget,并重写 paintEvent 方法。
参考
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout,
QScrollArea,
)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class PaintWidget(QWidget):
def __init__(self):
super().__init__()
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
qp.setPen(QPen(QColor(0, 0, 255), 3, Qt.DotLine))
r = event.rect()
qp.drawLine(r.left(), r.top(), r.right(), r.bottom())
qp.end()
class TopWidget(QWidget):
def __init__(self):
super().__init__()
self.setUI()
self.show()
def setUI(self):
self.setGeometry(300, 300, 800, 400)
self.topFiller = PaintWidget()
self.topFiller.