pythonpyqtdrawpaint

how to draw a rectangle from the center in PyQt6?


I want to draw a rectangle from the center of it like the below situation.

The image of rectangle:

the image of rectangle

painter.rectMode(Center)
painter.drawRect(x, y, w, h)

How I can do that task?

I want to draw a rectangle from the center. Please help me.


Solution

  • It is a simple geometry calculus that can handle by a simple function

    from PyQt6.QtGui import QPaintEvent, QPainter, QPen, QBrush, QColor
    from PyQt6.QtWidgets import QWidget, QApplication
    from PyQt6.QtCore import Qt
    from sys import argv, exit
    
    
    class Window(QWidget):
        def __init__(self, parent=None) -> None:
            super().__init__(parent)
            self.screen_width = 1920
            self.screen_height = 1080
            self.window_width = 700
            self.window_height = 400
            self.setGeometry(
                (self.screen_width - self.window_width) // 2,
                (self.screen_height - self.window_height) // 2,
                self.window_width,
                self.window_height,
            )
            self.show()
    
        def drawRectFromCenter(self, x, y, w, h):
            return x - w // 2, y - h // 2, w, h
    
        def paintEvent(self, a0: QPaintEvent | None) -> None:
            painter = QPainter()
            painter.begin(self)
            painter.setPen(QPen(QColor(0, 0, 0), 0, Qt.PenStyle.SolidLine))
            # draw rectangle from top-left corner
            x_from_top_left = 100
            y_from_top_left = 100
            w_from_top_left = 200
            h_from_top_left = 200
            r_from_top_left = 10
            painter.setBrush(QBrush(QColor(0, 255, 0), Qt.BrushStyle.SolidPattern))
            painter.drawRect(
                x_from_top_left, y_from_top_left, w_from_top_left, h_from_top_left
            )
            painter.setBrush(QBrush(QColor(255, 0, 0, 128), Qt.BrushStyle.SolidPattern))
            painter.drawEllipse(
                x_from_top_left - r_from_top_left // 2,
                y_from_top_left - r_from_top_left // 2,
                r_from_top_left,
                r_from_top_left,
            )
            # draw rectangle from center
            x_from_center = 500
            y_from_center = 200
            w_from_center = 200
            h_from_center = 200
            r_from_center = 10
            painter.setBrush(QBrush(QColor(0, 255, 0), Qt.BrushStyle.SolidPattern))
            x, y, w, h = self.drawRectFromCenter(
                x_from_center, y_from_center, w_from_center, h_from_center
            )
            painter.drawRect(x, y, w, h)
            painter.setBrush(QBrush(QColor(255, 0, 0, 128), Qt.BrushStyle.SolidPattern))
            painter.drawEllipse(
                x_from_center - r_from_center // 2,
                y_from_center - r_from_center // 2,
                r_from_center,
                r_from_center,
            )
            return super().paintEvent(a0)
    
    
    if __name__ == "__main__":
        App = QApplication(argv)
        window = Window()
        exit(App.exec())
    
    

    The window will look like this.

    The window.