0

I have a function that read from a JSON file and display the content in a QtextEdit using Pyqt5.

Problem is that when i tried to parse the content in the TextEdit the last record of the File Name is displayed.

while if i print to the console all the records of the File Name are printed as it should.

at the end i need to display the result as the comment print

def displayReport(self,item):
        searchRes=os.path.join(os.getcwd(),"search_result")
        path = os.listdir(searchRes)
        data =[]
        try:
            for file in path:            
                rpjson = json.load(open(os.path.join(searchRes,item)))
                for js in rpjson:
                    fileName = js["File Name"]
                    srchwRD = js["Searched Word"]
                    nbrOfOccur = str(js["Number Of Occurence"])
                    result = [fileName + srchwRD + nbrOfOccur]

                    print("this is file name {}".format(fileName))

                    data.append(result)


                    #print("****************" + "\n" + "File Name: " + 
                                          #js["File Name"] + "\n" + "Searched Word: " +
                                          #js["Searched Word"] + "\n" + "Number Of Occurence: " +
                                          #str(js["Number Of Occurence"]))

        except Exception as e:
            print("can't read JSON because {}".format(e))

        self.textEdit.setHtml("\n".join (data))        
Propy Propy
  • 57
  • 10
  • Indent `self.textEdit.setHtml(str(fileName))` – MegaIng Apr 28 '18 at 10:48
  • The `print` and `textEdit` are at different places, that's why they do different things. Maybe you want to indent the `textEdit` line into the loop? Obviously when you exit the loop `js` will point to the last file used. – kabanus Apr 28 '18 at 10:48
  • i put self.textEdit.setHtml(str(fileName)) on the same level of print inside the loop and still only displaying the last record – Propy Propy Apr 28 '18 at 10:55

1 Answers1

1

You are not "adding" to the textedit, instead you are replacing its content on each iteration of the loop - only the last content sticks.

Change

self.textEdit.setHtml(str(fileName)) # replaces the whole content of the qtestedit

to addding to its current content instead of replacing it.

Possbible solutions

See this SO post: How to append text to QPlainTextEdit without adding newline, and keep scroll at the bottom? for how to achieve this. (maybe you should add a newline between file names - you can adapt the solutions).

Maybe better way to do it:

  • Have a look at the API, append might also be a valid choice:QTextEdit.append(...)

  • collect all the text you need into a normal list of strings and set the QTextEdit only once after you visited all files. This should be faster performane wise as well as Qt does not update its element multiple times and has to process triggers and signals on it:


def displayReport(self,item):  # code untested, no mvce provided, may need small fixup(s)
    foundText = []
    searchRes=os.path.join(os.getcwd(),"search_result")
    path = os.listdir(searchRes)
    try:
        for file in path:            
            rpjson = json.load(open(os.path.join(searchRes,item)))
            for js in rpjson:
                fileName = js["File Name"]
                print("this is file name {}".format(fileName))

                foundText.append(fileName)   # add to list, join later, faster then
                                             # add to the immutable string multiple times
    except Exception as e:
             print("can't read JSON because {}".format(e))

    self.textEdit.setHtml('\n'.join(foundText))  # set text only once after processing

I would probably go for the last option (collecting into list + set once) as it minimizes Signals and Triggers.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69