Avoid unnecessary repaints
Unsolved
Qt for Python
-
Hello!
When I update label text in the example below all of the items in the list are repainted. WithoutQTabWidget
(with plainQWidget
, for example) thepaintEvent
is not triggered when text size is unchanged, but inside of a tab repaint happens after each label update.
Is there a way to avoid these repaints withQTabWidget
?
Example:from PyQt6.QtCore import QModelIndex from PyQt6.QtCore import QTimer from PyQt6.QtGui import QPainter from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QHBoxLayout from PyQt6.QtWidgets import QLabel from PyQt6.QtWidgets import QListWidget from PyQt6.QtWidgets import QMainWindow from PyQt6.QtWidgets import QStyledItemDelegate from PyQt6.QtWidgets import QStyleOptionViewItem from PyQt6.QtWidgets import QTabWidget from PyQt6.QtWidgets import QWidget class Delegate(QStyledItemDelegate): def __init__(self) -> None: QStyledItemDelegate.__init__(self) self.counter = 0 def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: print("paint!", self.counter) self.counter += 1 QStyledItemDelegate.paint(self, painter, option, index) class Widget(QWidget): def __init__(self) -> None: QWidget.__init__(self) self.label = QLabel() self.label.setText("00") self.list_widget = QListWidget() self.list_widget.setItemDelegate(Delegate()) self.list_widget.addItems(map(str, range(100))) self.list_widget.setViewMode(QListWidget.ViewMode.IconMode) hbox = QHBoxLayout() hbox.addWidget(self.label) hbox.addWidget(self.list_widget) self.setLayout(hbox) self.counter = 0 self.timer = QTimer() self.timer.timeout.connect(self.on_timeout) self.timer.start(1000) def on_timeout(self) -> None: self.counter += 1 self.label.setText(f"{self.counter:02}") class Window(QMainWindow): def __init__(self) -> None: QMainWindow.__init__(self) self.widget = Widget() self.tabs = QTabWidget() self.tabs.addTab(self.widget, "asdf") self.setCentralWidget(self.tabs) def main() -> int: app = QApplication(["asdf"]) w = Window() w.show() return app.exec() if __name__ == "__main__": raise SystemExit(main())
-
You may try:
- get visial rect of the item
QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const - update it only
void QWidget::update(const QRect &rect)
check here out
https://www.qtcentre.org/threads/1032-repaint-a-cell-or-row-in-QTableWidget - get visial rect of the item