Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Show text file in QTextEdit



  • Hi,

    I'm developing a text editor.
    I want to display a text file in QTextEdit.
    I tried the following:

            def menuoffnen():
                dateiname = QFileDialog.getOpenFileName(self, 'Datei öffnen', '', 'Textdateien (*.txt)')
                dateiname = dateiname[0]
                dateininhalt = open(dateiname).readlines().__str__()
                self.texteditor.setText(dateininhalt)
    
    

    The result:
    ['This is a test \ n', 'This is a test2 \ n', 'This is a test3']

    My problem:
    The square brackets and the characters "\ n"

    Question:
    What does the professional solution look like?


  • Qt Champions 2019

    I'm pretty sure you don't see the linebreaks in the textedit. What you show us is the output of 'print dateiinhalt'.



  • I describe the process:
    I save following text in windows nodepad:
    Markus Lemcke
    Adolf-Damaschke-Strasse 25/42
    72770 Reutlingen

    Then I open the same file in my Python Qt Texteditor and it looks so:
    ['Markus Lemcke\n', 'Adolf-Damaschke-Strasse 25/42\n', '72770 Reutlingen']

    Question:
    What's wrong with my function def menuoffnen() ?


  • Qt Champions 2019

    Maybe you convert them to a list of strings with readlines() instead simply reading all and putting it as single string into the textedit. @JonB: Am I right? Python does strange implicit conversions here (at least it looks like for me)



  • @PythonQTMarlem said in Show text file in QTextEdit:

    dateininhalt = open(dateiname).readlines().str()

    This is a Python issue. What you have here is a raw representation of a Python list of lines. That is why you get the [ ... ]; the \n comes from Python readlines(). It is not what you want to put into a QTextEdit.

    It's for you to work out in Python, but I would think something like:

    text = ''.join(open(dateiname).readlines())
    self.texteditor.setText(text)
    

    would do the job?

    Having said that, I'm thinking you can do the job without any need to do Python reading of individual lines. Does the following one-liner also do it right:

    self.texteditor.setText(open(dateiname).read())
    

    ?



  • text = ''.join(open(dateiname).readlines())
    self.texteditor.setText(text)
    

    You're a hero! It works. Thanks!



  • @PythonQTMarlem
    Did you try the second one, it's even simpler & quicker? :)



  • @PythonQTMarlem said in Show text file in QTextEdit:

    dateininhalt = open(dateiname).readlines().str()

    Yes, Thank you!



  •         def menuoffnen():
                dateiname = QFileDialog.getOpenFileName(self, 'Datei öffnen', '', 'Textdateien (*.txt)')
                dateiname = os.path.abspath(dateiname[0])
                dateininhalt = ''.join(open(dateiname, encoding="utf8").readlines())
                self.texteditor.setText(dateininhalt)
    
    

    I still have a problem:
    if I want to open a text file with the 8kb size with the code above, the program crashes with
    Process finished with exit code -1073740791 (0xC0000409)

    Why is that?



  • @PythonQTMarlem
    I don't know what your problem is/why it crashes, it really won't be anything about " the 8kb size". Try it with different files/file content, your problem must lie elsewhere. Put in print() messages to see where your program is actually getting to before it crashes, it really should be elsewhere than in this code.


Log in to reply