簡體   English   中英

如何在python中使用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

我需要的只是返回所選的項目。

這樣,無論存在何處,用戶都可以選擇任何項目。

例如:

  • C:\\ Users \\ test \\ Desktop \\ New Microsoft Word Document.docx行=> 0
  • C:\\ Users \\ test \\ Desktop \\ test_arabic.docx行=> 1

在下面的函數中,我嘗試遍歷QlistWidget中的項目

  1. 獲取文件完整路徑(從根目錄)
  2. 分割道路
  3. 根據索引添加其余路徑

但這不起作用,因為有時列表中顯示的文件來自不同的文件夾,因此它們具有不同的路徑。

碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM