v_sith_v
v_sith_v

Reputation: 139

How to resize QWidget without intermediate paint

During the study, Qt encountered such a problem. Suppose I have a QWidget on the QMainWindow. How can I make sure that when I resize QMainWindow, QWidget on this QMainWindow do not repaint content until the resizing does not stop.

Yeah, I saw this example How to disable multiple auto-redrawing at resizing widgets in PyQt?

But when I tried this method it's just locked widget content. I just wondered if it was possible to make sure that the contents of the QWidget did not change while we resizing MainWindow. Please tell me, is this possible?

Thanks a lot.

Upvotes: 0

Views: 666

Answers (1)

G.M.
G.M.

Reputation: 12929

I'm still guessing slightly as to what you want exactly but it sounds as if you essentially want two modes for your paintEvent method -- the normal one that takes care of rendering the widget most of the time and a second, lightweight, mode that can be used whilst the widget is being resized.

If that's the case then you could try something like the following...

#!/usr/bin/python3
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class widget(QWidget):
    def __init__(self):
        super().__init__()
        self.resize_timer = QTimer(self)
        self.resize_timer.setSingleShot(True)
        self.resize_timer.setInterval(100)
        self.resize_timer.timeout.connect(self.delayed_update)

    def delayed_update(self):
        self.update()

    def paintEvent(self, event):
        if self.resize_timer.isActive():
            print("painting deferred")

            # Your `lightweight' rendering goes here and will be used
            # while the widget is being resized.
        else:
            print("painting now")

            # Full rendering code goes here.

    def resizeEvent(self, event):
        super(widget, self).resizeEvent(event)
        self.resize_timer.start()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    f = widget()
    f.show()
    sys.exit(app.exec_())

Note that it is essentially just a simple modification of the code in the answer you linked to.

Upvotes: 2

Related Questions