How to disable multiple auto-redrawing at resizing widgets in PyQt?
Asked Answered
D

1

5

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.

Dachy answered 25/11, 2012 at 15:18 Comment(5)
Is this a result of custom paint events or are these default widgets?Marianelamariani
@jdi, default widgets on main window. For example, imagine that I'll trying to resize from QTreeWidget to QListWidget.Dachy
But a QTreeWidget is only going to paint the items that are visible. Do you have it loaded up heavily with custom cell widgets?Marianelamariani
@jdi, I'll just trying to tell that it's default widgets, no matter which specifically. And there are no any custom paint events.Dachy
The reason I asked is because if you are loading up a view with a bunch of widgets, that will cause performance issues. But I do understand that you are looking for a generic solution. My answer below talks about handling a resize event.Marianelamariani
M
9

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.

Marianelamariani answered 25/11, 2012 at 20:14 Comment(4)
Well, thank you for your commit, it's helpful workaround! But there are still some problems. Firstly, for some reasons your example suspends after a few "actual updates" (main window movings). And then it looks like this: i.imgur.com/pnY2m.jpg and shows only crap. Secondly, your workaround helps only with main window movings. Which is good, but window movings is only the one of the redrawing cases. Also there are QSplitter movings and QDockWidget movings, and this seems now working with it. But unfortunately exactly those two i need the most :( sincerely yours.Dachy
This is not specific to main window. It can work for any QWidget. You are just watching the resize event in this case. There is no global opaque resize option.Marianelamariani
@mivulf: Sorry. I left a line of code in there that should not be there. I removed the processEvents call. That was only for testing and crashed for me as well. It shouln't be locking up at this point. Let me know if you need to see an example of this a a utility QObject that uses event filterMarianelamariani
wow! ^_^ Thank you so much! It works perfectly! Now I'm happy. But I can't give you up-vote, because I haven't enough reputation yet.Dachy

© 2022 - 2024 — McMap. All rights reserved.