Как у следующего окна сделать event изменения размера и координат для QRect: class…
- User
Как у следующего окна сделать event изменения размера и координат для QRect: class MainWindow(QMainWindow): def init(self, coordinates): super().init() self.setWindowTitle("Transparent Drawing Window") self.setGeometry(0, 0, QApplication.primaryScreen().size().width(), QApplication.primaryScreen().size().height()) self.setAttribute(Qt.WA_TranslucentBackground, True) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.painter = QPainter() self.painter.setRenderHint(QPainter.Antialiasing) self.pen_color = QColor(255, 0, 0) # Set the initial pen color to red self.pen_width = 4 # Set the initial pen width to 4 self.coordinates = coordinates # Store the coordinates for drawing rectangles self.starting_point = None #self.red_button = QPushButton('Red', self) #self.red_button.clicked.connect(self.set_red_color) #self.blue_button = QPushButton('Blue', self) #self.blue_button.clicked.connect(self.set_blue_color) self.dragLineEdit = QLineEdit(self) #self.dragLineEdit.setEnabled(False) self.dragLineEdit.setReadOnly(True) def set_red_color(self): self.pen_color = QColor(255, 0, 0) self.update() def set_blue_color(self): self.pen_color = QColor(0, 0, 255) self.update() def paintEvent(self, event): self.painter.begin(self) self.painter.setPen(Qt.NoPen) self.painter.setBrush(QBrush(Qt.transparent)) self.painter.drawRect(QRect(0, 0, self.width(), self.height())) # Draw a transparent background self.painter.setPen(QPen(QColor(self.pen_color), self.pen_width)) self.painter.setBrush(QBrush(Qt.transparent)) #self.red_button.move(self.coordinates[0], self.coordinates[1]) #self.red_button.resize(50, 50) #self.blue_button.move(self.coordinates[0] + 50, self.coordinates[1]) #self.blue_button.resize(50, 50) self.dragLineEdit.move(self.coordinates[0], self.coordinates[1]) self.dragLineEdit.resize(self.coordinates[2], 20) self.painter.drawRect(self.coordinates[0], self.coordinates[1], self.coordinates[2], self.coordinates[3]) # Draw rectangles using the provided coordinates self.painter.end() - Assistant
Чтобы установить событие изменения размера и координат для QRect в вашем окне, вам необходимо переопределить методы
resizeEventиmoveEventв классеMainWindow. В этих методах вы можете обновлять свойствоself.coordinatesи перерисовывать окно при изменении размера или перемещении окна.Ниже приведен пример кода, где переопределены методы
resizeEventиmoveEvent:class MainWindow(QMainWindow): def __init__(self, coordinates): super().__init__() self.coordinates = coordinates # Store the initial coordinates # Остальной код def resizeEvent(self, event): self.coordinates[2] = event.size().width() # Update the width self.coordinates[3] = event.size().height() # Update the height self.update() def moveEvent(self, event): self.coordinates[0] = event.pos().x() # Update the x coordinate self.coordinates[1] = event.pos().y() # Update the y coordinate self.update()После добавления вышеприведенного кода, при изменении размера или перемещении окна, свойство
self.coordinatesбудет обновляться, и окно будет перерисовываться с новыми координатами и размером. - User
но он будет изменять в том случае если изменяется окно, но окно здесь прозрачное
- Assistant
- User
Как здесь добавить изменение размера QRect
- Assistant