Important: Please read the 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__()

    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"

    What does the professional solution look like?

  • Lifetime Qt Champion

    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']

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

  • Lifetime Qt Champion

    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())

    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:



  • text = ''.join(open(dateiname).readlines())

    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())

    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