Equivalent to wxPython freeze and push away in PyQt / PySide

When I make several widget updates in wxPython (for example, loading the contents of tables, trees, hiding widgets, etc.), I often use Freeze to disable widget re-drawing, and when I finish, I call Thaw so that the new content is displayed at all once. Is there a similar way in Qt?

Related question: Python / Tkinter: enable / disable screen updates such as wxPython Freeze / Thaw?

+4
source share
2 answers

This is not necessary in Qt, because update events are compressed and will not be delivered until the control returns to the event loop. For example, the following will result in only one redraw of the label:

void test(QLabel * label) {
  label->setText("foo");
  // update is called internally by setText, but the extra call is harmless here
  label->update(); 
  label->setText("bar");
  label->setText("baz");
}

If you are sure that your updates alternate with returns to the event loop, then you can implement freeze / thaw as follows by filtering update events. The following is in C ++, feel free to translate it into Python :)

class UpdateFilter : public QObject {
  Q_OBJECT
  QSet<QObject> m_deferredUpdates;
  Q_SLOT void isDestroyed(QObject * obj) {
    m_deferredUpdates.remove(obj);
  }
  Q_OBJECT bool eventFilter(QObject * obj, QEvent * ev) {
    if (ev->type() == QEvent::UpdateRequest) {
      if (! m_deferredUpdates.contains(obj) {
        m_deferredUpdates.insert(obj);
        connect(obj, SIGNAL(destroyed(QObject*)), SLOT(isDestroyed(QObject*)));
      }
      return true;
    }
    return false;
  }
public:
  void thaw(QWidget * w) {
    if (m_deferredUpdates.contains(w)) {
      w->update();
      m_deferredUpdates.remove(w);
    }
};

// This instance is only created on the first call to freeze.
Q_GLOBAL_STATIC(UpdateFilter, updateFilter)

void freeze(QWidget * w) { w->installEventFilter(&updateFilter()); }
void thaw(QWidget * w) { updateFilter().thaw(w); }
+2
source

I know this is an old post, but I found it on Disable PyQt event loop when editing a table

There is a way to do this:

blockSignals(bool) QObjects , . QObject. , - , , .

, ( , ), updatesEnabled(bool). , , . , .

mainWidget.setUpdatesEnabled(False)
# do a bunch of operations that would trigger expensive events
# like repaints
mainWidget.setUpdatesEnabled(True)
+2

Source: https://habr.com/ru/post/1532333/


All Articles