繁体   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