如何在 PyQt 中调整小部件大小时禁用多次自动重绘? [英] How to disable multiple auto-redrawing at resizing widgets in PyQt?

查看:53
本文介绍了如何在 PyQt 中调整小部件大小时禁用多次自动重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有小部件的 PyQt4 程序,其内容重绘非常缓慢(没关系,因为我的任务).当我尝试调整这些小部件的大小时,程序会在未释放鼠标的情况下尝试重绘很多次.这是很多冻结.

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.

我想禁用自动重绘并将 PyQt 配置为仅在释放鼠标时重绘所有小部件(这意味着每次调整大小都会重绘一次).

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).

怎么做?

Edit1. 我会很简单地看到它,就像这样:您拖动线条,并且在拖动时,所有小部件都会站立.当您释放它时,小部件会重新绘制.但我真的不确定 PyQt4 是否可行.

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...

绘制不透明的决定是由您平台的窗口管理器做出的.据我所知,没有一个简单的属性来切换这个功能.QSplitter 仅在释放手柄后进行绘制.

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_()

您会注意到,当您快速调整主窗口的大小时,自定义小部件不会发生更新,因为我们已在调整大小事件中将其关闭.QTimer 试图每 100 毫秒触发一次以执行更新并停止自身.但是每次发生另一个调整大小事件时,它都会重新启动该计时器.效果是计时器将继续重置.禁用更新,直到发生延迟.

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.

尝试按住鼠标,稍微调整大小,等待,然后再调整一些大小.即使在鼠标按下但未调整大小时也应进行更新.调整延迟以适应.您可以使用 bool 标志来控制打开和关闭该功能.

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.

这个例子也可以重新设计,使 DelayedUpdater 只是一个 QObject,它接受一些 QWidget 实例作为参数.然后它将自己设置为该对象的 eventFilter 并监视其 resizeEvent.这样你就不必为了添加它而对普通小部件进行子类化.您只需创建一个 DelayedUpdater 实例并将其用作实用程序对象来监视小部件.

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)

DelayedUpdater 监视目标小部件的大小调整事件,并执行延迟更新.您可以展开 eventFilter 以观察其他事件,例如移动.

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.

这篇关于如何在 PyQt 中调整小部件大小时禁用多次自动重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆