python-从PyQt5中的QListWidget删除项目
作者:互联网
我正在尝试在pyqt5中创建一个刷新按钮.我正在构建一个桌面应用程序.我编写了扫描特定文件夹并将文件名及其路径保存为数组的代码.
数组值作为项目添加到QListWidget
self.sampleChoose_list.addItems(sample_directory[0])
我正在尝试制作一个刷新数组值并将其传递给QListWidget的函数.
像这样
self.refreshSamples.clicked.connect(self.refreshSample)
def refreshSample(self):
sample_directory = []
sample_files = []
for (dirpath, dirnames, filenames) in walk('./Samples'):
filenames = [f for f in filenames if not f[0] == '.']
sample_files.extend(filenames)
break
the_dir = "Samples"
paths = [os.path.abspath(os.path.join(the_dir,filename)) for filename in os.listdir(the_dir) if not filename.startswith('.')]
sample_directory.append(sample_files)
sample_directory.append(paths)
self.sampleChoose_list.addItems(sample_directory[0])
我遇到的问题是:当我按下“刷新”按钮时,会添加新项,但不会删除旧项.如何从QListWidget删除项目?
解决方法:
使用QListWidget.clear()和它
Removes all items and selections in the view.
要获得更好的选择,您可以使用QListWidget.takeItem(index),它
Removes and returns the item from the given row in the list widget
Official Docs: clear()
Official Docs: takeItem()
标签:pyqt5,pyqt,desktop-application,python 来源: https://codeday.me/bug/20191025/1927767.html