我有一个代码,在QlistWidget中显示项目,一旦用户选择任何文件,系统仅返回最后一个项目,就会引用文件路径。
我需要的是仅返回所选项目。
这样用户就可以选择任何项目,无论它存在于何处。
例:
- C:\用户\测试\桌面\新建Microsoft Word 文档.docx行 =>0
- C:\用户\测试\桌面\test_arabic.docx行 =>1
在下面的函数中,我正在尝试迭代 QlistWidget 中的项目
- 获取文件的完整路径(从根目录(
- 拆分路径
- 根据索引添加路径的其余部分
但这不起作用,因为有时列表中的显示文件来自不同的文件夹,因此具有不同的路径。
法典:
def FileListSelected(self): # Function to select the desired file from the list in the left pane
ListIterator=range(self.listWidgetPDFlist.count())
try:
index = 0
for index in ListIterator:
p = pathlib.Path(self.fullPath)
print("this is P==>{}".format(p))
oneDir = os.path.join(*p.parts[:-2])
print("this is oneDir==>{}".format(oneDir))
Item= oneDir + "\" + self.listWidgetPDFlist.selectedItems()[index].text()
print("this is the cuurent Item =={}".format(Item))
ppp=os.path.join(os.path.expanduser("~"), Item)
print("this is ppp==>{}".format(ppp))
print("===============================================")
index =+1
print("index =>".format(index))
self.mouseHover()
return ppp
except Exception as e:
print(e)
如果我理解正确,您希望从列表中获取所选项目的文本。 为此,请使用:
self.listWidgetPdfList.selectedItems()[0].text()
在这里,您使用 0 作为索引,因为您只需要所选项目列表中的第一项。 如果只需要所选项,则无需循环访问列表。