[Python] resizeEvent keeps on retriggering

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I have a problem when I try to do some dynamic adjustments on my window or in my screen resolution.

    Inside resizeEvent I'm doing this:

    def resizeEvent(self, event):
    super().resizeEvent(event)

    # Calculate button height dynamically based on the window's height
    button_height = self.height() // 15
    button_width = self.width() // 2

    # Set dynamic size for buttons
    self.scan_button.setFixedHeight(button_height)
    self.pause_button.setFixedHeight(button_height)
    self.settings.setFixedHeight(button_height)
    self.saveCSV.setFixedHeight(button_height)
    self.savePNG.setFixedHeight(button_height)

    # Optionally, adjust button widths proportionally
    self.scan_button.setFixedWidth(button_width)
    self.pause_button.setFixedWidth(button_width)
    self.settings.setFixedWidth(button_width)
    self.saveCSV.setFixedWidth(button_width)
    self.savePNG.setFixedWidth(button_width)


    The thing is, when I try to run it, it triggers a re-render in a loop. Also, this happens after a specific value is exceeded (for my case the value 2 it will trigger a re-render but the value of 3 will not in my button_width).

    Some of my UI widgets inside my init:

    # ============================== Create a Grid Layout for the Buttons ==============================
    button_layout = QGridLayout()
    self.scan_button = QPushButton("Scan")
    self.pause_button = QPushButton("Pause")
    self.settings = QPushButton("Settings")
    self.saveCSV = QPushButton("Save CSV")
    self.savePNG = QPushButton("Save PNG")

    self.pause_button.setVisible(False)

    button_layout.addWidget(self.scan_button, 0, 0) # Row 0, Column 0
    button_layout.addWidget(self.pause_button, 0, 0) # Row 0, Column 1
    button_layout.addWidget(self.settings, 1, 0) # Row 0, Column 1
    button_layout.addWidget(self.saveCSV, 0, 1) # Row 0, Column 1
    button_layout.addWidget(self.savePNG, 1, 1) # Row 0, Column 1

    layout.addLayout(button_layout, 1)

    self.setLayout(layout)

    Continue reading...

Compartilhe esta Página