Rao
Rao

Reputation: 2962

How to update QListWidget

How to update QLIstWidget to show items as when when it is added.

Like i am adding 100 QListWidgetItems to QListWidget in a loop. All these 100 items are visible only after loop is completed. But i want to know if is it possible to show item as and when the item is added.

I tried self.ListWidget.setUpdatesEnabled(True) but no luck.

Any help is appreciated.

Upvotes: 3

Views: 10226

Answers (1)

a_manthey_67
a_manthey_67

Reputation: 4286

you can repaint the listwidget in the loop:

def insertItem(self):
    for i in range(1,100):
        self.listWidget.addItem(str(i))
        self.listWidget.repaint()

with QTimer you can control the delay between 2 items.

Edit: Perhaps i didn't understand your question correctly: you can add all items, hide them and then set them visible item by item:

import sys 
from PyQt5 import QtGui, QtCore, QtWidgets

class MyWidget(QtWidgets.QWidget): 
    def __init__(self): 
        QtWidgets.QWidget.__init__(self) 
        self.setGeometry(200,100,600,900)
        self.listWidget = QtWidgets.QListWidget(self)
        self.listWidget.setGeometry(20,20,100,700)
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setGeometry(20,800,100,30)
        self.pushButton.setText('show Items')
        self.pushButton.clicked.connect(self.showItems)
        self.timer = QtCore.QTimer()
        for i in range(0,100):
            self.listWidget.addItem(str(i))
            self.listWidget.item(i).setHidden(True)
        self.z = 0

    def showItems(self):
        self.timer.start(100)
        self.timer.timeout.connect(self.nextItem)    

    def nextItem(self):
        try:
            self.listWidget.item(self.z).setHidden(False)
            self.listWidget.repaint() 
            self.z += 1
        except AttributeError:
            self.timer.stop()
            self.z = 0

app = QtWidgets.QApplication(sys.argv) 
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

in pyqt4 replace 'QtWidgets' by 'QtGui'

Upvotes: 4

Related Questions