I have a PyQt4 program with widgets whose content redraws very slowly (it's ok, because of my tasks). And when I trying to resize those widgets, program is trying to redraw a lot of times while mouse is not released. That's a lot of freezes.
I want to disable that auto-redrawing and configure PyQt to redraw all widgets only when mouse is released (which means that redraw happens exactly one time per one resize).
How to do that?
Edit1. I'll see it quite simply, like this: you drag the line, and while you dragging, all widgets stand. When you release it, widgets redrawing. But I'm really not sure that it's possible in PyQt4.
First, I would recommend making sure that if you are using custom paint events with your widgets, that you are not doing too heavy of work in each event and simply looking for a band-aid solution. If this is the case, try and find a way to cache or reduce the work. Otherwise...
The decision to draw opaque or not is one made by the window manager of your platform. As far as I know, there is not a simple attribute to toggle this feature. Something similar to this exists on a QSplitter
to only draw after the handle is released.
I can offer one workaround approach, which is to delay the update until after no resize has occurred for a period of time. This will give your application some breathing room to reduce the paint events.
from PyQt4 import QtCore, QtGui
import sys
class DelayedUpdater(QtGui.QWidget):
def __init__(self):
super(DelayedUpdater, self).__init__()
self.layout = QtGui.QVBoxLayout(self)
self.label = QtGui.QLabel("Some Text")
self.layout.addWidget(self.label, QtCore.Qt.AlignCenter)
self.delayEnabled = False
self.delayTimeout = 100
self._resizeTimer = QtCore.QTimer(self)
self._resizeTimer.timeout.connect(self._delayedUpdate)
def resizeEvent(self, event):
if self.delayEnabled:
self._resizeTimer.start(self.delayTimeout)
self.setUpdatesEnabled(False)
super(DelayedUpdater, self).resizeEvent(event)
def _delayedUpdate(self):
print "Performing actual update"
self._resizeTimer.stop()
self.setUpdatesEnabled(True)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
view = DelayedUpdater()
win.setCentralWidget(view)
win.show()
view.delayEnabled = True
app.exec_()
You will notice that as you resize the main window quickly, no updates are occurring for the custom widget, because we have turned them off in the resize event. A QTimer is trying to fire every 100 ms to perform the update and stop itself. But each time another resize event occurs, it will restart that timer. The effect is that timer will continue to be reset. leaving updates disabled, until a delay occurs.
Try holding down the mouse, resizing a little, wait, and resize some more. The update should occur even while your mouse is down but you are not resizing. Adjust the delay to suit. And you have control over turning the feature on and off with the bool flag.
This example could also be re-worked to make DelayedUpdater
just a QObject, which accepts some QWidget instance as an argument. It would then set itself to be the eventFilter
for that object and monitor its resizeEvent
. That way you don't have to subclass normal widgets just to add this. You would simply make an instance of DelayedUpdater
and use it as a utility object to monitor the widget.
Here is an example of making it a helper object:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.someWidget = QtGui.QWidget()
self.setCentralWidget(self.someWidget)
self.layout = QtGui.QVBoxLayout(self.someWidget)
self.label = QtGui.QLabel("Some Text")
self.layout.addWidget(self.label, QtCore.Qt.AlignCenter)
self.delayer = DelayedUpdater(self.someWidget)
class DelayedUpdater(QtCore.QObject):
def __init__(self, target, parent=None):
super(DelayedUpdater, self).__init__(parent)
self.target = target
target.installEventFilter(self)
self.delayEnabled = True
self.delayTimeout = 100
self._resizeTimer = QtCore.QTimer()
self._resizeTimer.timeout.connect(self._delayedUpdate)
def eventFilter(self, obj, event):
if self.delayEnabled and obj is self.target:
if event.type() == event.Resize:
self._resizeTimer.start(self.delayTimeout)
self.target.setUpdatesEnabled(False)
return False
def _delayedUpdate(self):
print "Performing actual update"
self._resizeTimer.stop()
self.target.setUpdatesEnabled(True)
Note that we are using this on just some arbitrary widget inside of our main window. We add a delay updater to it with this line:
self.delayer = DelayedUpdater(self.someWidget)
The DelayedUpdater
watches the resize events of the target widget, and performs delayed updates. You could expand the eventFilter
to also watch for other events, like a move.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With