0

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.

m7913d
  • 10,244
  • 7
  • 28
  • 56
v_sith_v
  • 109
  • 1
  • 1
  • 7
  • If the answer you've linked to isn't what you're looking for then you need to explain in more detail precisely what you want. – G.M. Jun 21 '17 at 10:28
  • Well, when we doing like in example from link we have following situation. When we start resize, content in QWidget just hide and it unhide when we stopping resize main window. So, while we resizing QMainWindow, content of the QWidget is simply invisible, which is not good. I just wanna know, there may be a way to ensure that the content does not just change during the resize and repaint when we finish resizing – v_sith_v Jun 21 '17 at 10:43
  • So, what do you expect to see in the widget when it's resizing? – G.M. Jun 21 '17 at 10:49
  • Maybe something like when we resizing QSplitters. We start resizing and сontent simply remains unchanged, and redrawn when we are done. Is this possible? – v_sith_v Jun 21 '17 at 10:57

1 Answers1

2

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.

G.M.
  • 12,232
  • 2
  • 15
  • 18