QPaint inside QGroupBox

204 Views Asked by At

I am trying to get a graphic where the drawing is put into a groupbox in pyqt5. Below is my code. If you run it, it will put the graphic in the main window and draw the edge of the groupbox over top of it but I'd like it to be inside the groupbox (because I plan on doing a couple different graphics in different boxes.

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_()

Lastly, I thought I could maybe change the line: paint.begin(self) to: paint.begin(self.groupGimble) but it didn't paint any of the graphic after that.

0

There are 0 best solutions below