Qt - Python - PyQt/PySide - QRect detailed analysis and method introduction

In actual development, it is often necessary to draw shapes, such as rectangles.
QRect is a class used to represent rectangles in PyQt5. It contains the coordinates, width, height and other information of the four corners of the rectangle, which can be used for drawing, layout and other operations.

The following are common methods and properties of the QRect class:

1. Constructor:

  • QRect(): Create an empty rectangle.
  • QRect(x, y, w, h): Creates a rectangle with the coordinates of the upper left corner at (x, y), width w, and height h.
  • QRect(QPoint topLeft, QPoint bottomRight): Creates a rectangle in which the top-left and bottom-right coordinates are specified by topLeft and bottomRight, respectively.
  • QRect(QRect other): Creates a rectangle identical to another rectangle other.

2. Member functions:

  • x(): Returns the x coordinate of the upper left corner of the rectangle.
  • y(): Returns the y coordinate of the upper left corner of the rectangle.
  • width(): Returns the width of the rectangle.
  • height(): Returns the height of the rectangle.
  • topLeft(): Returns the coordinates of the upper left corner of the rectangle.
  • bottomRight(): Returns the coordinates of the bottom right corner of the rectangle.
  • isNull(): Checks if the rectangle is null (zero width or height).
  • isEmpty(): Checks if the rectangle is empty (whether the width or height is less than or equal to zero).
  • normalized(): Returns a normalized rectangle, that is, the coordinates of the lower left corner are in the front and the coordinates of the upper right corner are in the back.

3. Geometric operations:

  • operator+ and operator+=: translate the rectangle to the bottom right or perform addition and subtraction operations.
  • operator- and operator-=: translate the rectangle to the bottom left or perform addition and subtraction operations.
  • operator* and operator*=: Scale or multiply the width and height of the rectangle.
  • operator/ and operator/=: Scale or divide the width and height of the rectangle.

4. Judging the relationship between rectangles:

  • operator== and operator!=: Compares two rectangles for equality or inequality.
  • contains(QPoint) and contains(QRect): Determine whether a point or another rectangle is inside the current rectangle.
  • intersects(QRect): Determine whether the current rectangle intersects with another rectangle.

5. Utility functions:

  • adjusted(dx, dy, dw, dh): Returns a new rectangle obtained by adjusting the position and size of the current rectangle. Can move position on dx and dy, resize on dw and dh.
  • normalized(): Returns a normalized rectangle, that is, the coordinates of the lower left corner are in the front and the coordinates of the upper right corner are in the back.
    These methods and properties can help us create, manipulate and process rectangle-related geometric figures, such as using QRect in PyQt5 for drawing, layout, collision detection and other operations.

Use QPen combined with QRect to draw a rectangle:

from PyQt5.QtWidgets import QApplication, QWidget, QRect, QPainter, QPen  
  
class MyWidget(QWidget):  
    def __init__(self):  
        super().__init__()  
        self.setGeometry(100, 100, 200, 200)  
        self.show()  
  
    def paintEvent(self, event):  
        painter = QPainter(self)  
        rect = QRect(20, 20, 160, 160)  
        pen = QPen()  
        pen.setColor(Qt.red)  
        pen.setWidth(2)  
        painter.setPen(pen)  
        painter.drawRect(rect)  
  
if __name__ == '__main__':  
    app = QApplication([])  
    widget = MyWidget()  
    app.exec_()

In this example, we create a custom QWidget derived class MyWidget, and use QPainter and QPen to draw a red rectangle in its paintEvent() method. First, we create a QRect object representing the position and size of the rectangle. Then, we create a QPen object and set its color and width. Finally, we use QPainter's setPen() method to set the QPen object as the brush for drawing the rectangle, and use the drawRect() method to draw the rectangle.

It should be noted that when using QPen to draw shapes, the QPen object must be set as the brush of QPainter to use the brush to draw shapes.

Guess you like

Origin blog.csdn.net/weixin_44697721/article/details/131801794