本文介绍了如何更新 QListWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新 QLIstWidget 以在添加项目时显示项目.

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

就像我在循环中向 QListWidget 添加 100 个 QListWidgetItems 一样.所有这 100 个项目只有在循环完成后才可见.但我想知道是否可以在添加项目时显示项目.

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.

我尝试了 self.ListWidget.setUpdatesEnabled(True) 但没有成功.

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

感谢任何帮助.

推荐答案

您可以在循环中重新绘制列表控件:

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()

使用 QTimer,您可以控制 2 个项目之间的延迟.

with QTimer you can control the delay between 2 items.

也许我没有正确理解你的问题:您可以添加所有项目,隐藏它们,然后将它们逐项设置为可见:

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_())

在pyqt4中用'QtGui'替换'QtWidgets'

in pyqt4 replace 'QtWidgets' by 'QtGui'

这篇关于如何更新 QListWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 08:37