How to update QListWidget

本小妞迷上赌 提交于 2019-12-01 11:21:41

问题


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.


回答1:


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'



来源:https://stackoverflow.com/questions/28536520/how-to-update-qlistwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!