我正在尝试获取将绘图放入 pyqt5 中的 groupbox 的图形。下面是我的代码。如果你运行它,它会将图形放在主窗口中并在其顶部绘制组框的边缘,但我希望它位于组框内(因为我计划在不同的框中制作几个不同的图形。
CENTERX = 80
CENTERY = 0
RADX = 50
RADY = 50
WIDTH = 160
HEIGHT = 120
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QSlider, QVBoxLayout, QHBoxLayout, QGridLayout
class DrawCircles(QtWidgets.QWidget):
def __init__(self, parent=None):
super(DrawCircles, self).__init__(parent, QtCore.Qt.WindowStaysOnTopHint)
self.setGeometry(300, 300, WIDTH, HEIGHT)
# Main Layout
self.mainLayout = QVBoxLayout(self)
self.groupGimble = QtWidgets.QGroupBox()
self.mainLayout.addWidget(self.groupGimble)
def paintEvent(self, event):
print(str(event.rect()))
self.drawCircle(event)
def drawCircle(self, event):
paint = QtGui.QPainter()
paint.begin(self)
# optional
paint.setRenderHint(QtGui.QPainter.Antialiasing)
# make a white drawing background
paint.setBrush(QtCore.Qt.white)
paint.drawRect(event.rect())
# draw red circles
paint.setPen(QtCore.Qt.black)
paint.setBrush(QtCore.Qt.black)
center = QtCore.QPoint(CENTERX, CENTERY)
paint.drawEllipse(center, RADX, RADY)
paint.end()
app = QtWidgets.QApplication([])
circles = DrawCircles()
circles.show()
app.exec_()
最后,我想我可以将行:更改
paint.begin(self)
为:
paint.begin(self.groupGimble)
但之后它没有绘制任何图形。