简体   繁体   English

如何获取所有项目存在于PyQt5中的QlistWidget中

[英]how to get the all items exist in QlistWidget in PyQt5

i have a function that display a list of files exist in selected directory then the user enter a searched word where the program read these file in the background in order to find the matching word, at the end it override the existing list by just displaying the files that include the matching word. 我有一个功能,显示所选目录中存在的文件列表,然后用户输入一个搜索词,程序在后台读取这些文件以找到匹配的词,最后它仅显示包含匹配单词的文件。

the problem is it the while loop the system display this error : 问题是while循环时系统显示此错误:

while index < len(self.listWidgetPDFlist.count()): 而索引<len(self.listWidgetPDFlist.count()):

builtins.TypeError: object of type 'int' has no len() Builtins.TypeError:类型为'int'的对象没有len()

code: 码:

def listFiles(self):

        readedFileList = []
        index = 0
        while index < len(self.listWidgetPDFlist.count()):
            readedFileList.append(self.listWidgetPDFlist.item(index))
        print(readedFileList)

        try:
            for file in readedFileList:

                with open(file) as lstf:
                    filesReaded = lstf.read()
                    print(filesReaded)
                return(filesReaded)

        except Exception as e:
            print("the selected file is not readble because :  {0}".format(e))     

count() returns the number of items so it is an integer, the function len( ) applies only to iterable, not integers, so you get that error, plus it is not necessary. count()返回项目数,因此它是一个整数,函数len( )仅适用于可迭代的对象,不适用于整数,因此您会收到该错误,并且没有必要。 you must do the following: 您必须执行以下操作:

def listFiles(self):
    readedFileList = [self.listWidgetPDFlist.item(i).text() for i in range(self.listWidgetPDFlist.count())]
    try:
        for file in readedFileList:
            with open(file) as lstf:
                filesReaded = lstf.read()
                print(filesReaded)
                # return(filesReaded)

    except Exception as e:
        print("the selected file is not readble because :  {0}".format(e)) 

Note: do not use return, you will have the loop finish in the first iteration. 注意:请勿使用return,您将在第一次迭代中完成循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM