问题描述
我正在尝试在pyqt5中创建一个刷新按钮。我正在构建一个桌面应用程序。我编写了扫描特定文件夹并将文件名及其路径保存为数组的代码。
I am trying to make a refresh button in pyqt5. I am building a desktop app. I wrote the code that scans particular folder and saves filenames and their paths as an array.
数组值作为项添加到QListWidget
Array values are added to QListWidget as items
self.sampleChoose_list.addItems(sample_directory[0])
我正在尝试创建一个刷新数组值并将其传递给QListWidget的函数。
I am trying to make a function that refreshes values of the array and passes it to 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中删除项目?
The problem that I struggle with is: when I push refresh button, new items get added, but the old ones are not deleted. How to remove items from QListWidget?
推荐答案
使用 QListWidget.clear()
并且它
要获得更多选择性,您可以使用 QListWidget.takeItem(index)
,
for am more selective approuch you can use QListWidget.takeItem( index )
, it
这篇关于从PyQt5中的QListWidget删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!