[英]How to trigger a function after clicking on a list item in a QListWidget? PyQt5
[英]how to return the selected Item in the list and not the last Item in the list using Pyqt5 in python
我有一個代碼,一旦用戶選擇任何文件, 系統將僅返回最后一個Item ,從而在QlistWidget中顯示引用文件路徑的Items 。
我需要的只是返回所選的項目。
這樣,無論存在何處,用戶都可以選擇任何項目。
例如:
在下面的函數中,我嘗試遍歷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用作索引,因為您只希望所選項目列表中的第一項。 如果只需要選定的項目,則不需要遍歷列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.