Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Avoid unnecessary repaints
Forum Updated to NodeBB v4.3 + New Features

Avoid unnecessary repaints

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 290 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xueqli
    wrote on last edited by xueqli
    #1

    Hello!
    When I update label text in the example below all of the items in the list are repainted. Without QTabWidget (with plain QWidget, for example) the paintEvent 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 with QTabWidget?
    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())
    
    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      You may try:

      1. get visial rect of the item
        QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const
      2. 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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved