Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function
Forum Updated to NodeBB v4.3 + New Features

[PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 774 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rex Li
    wrote on last edited by Rex Li
    #1

    Hi everyone,

    Here is my problem.
    I'm writing a Python application with PySide2. In the code, I open a text file. The text file has 160k lines. I did some operations on each line and concatenated each line together to a very long string. Each line was separated by "\n". I call QTextEdit.setPlainText() to set the very long string into the QTextEdit.
    It is very slow when scrolling the scroll bar or even got "not responding" eventually.

    Could you please give me any suggestions to speed it up or solve the problem?

    Thanks a lot!
    Rex

    JonBJ 1 Reply Last reply
    0
    • R Rex Li

      Hi everyone,

      Here is my problem.
      I'm writing a Python application with PySide2. In the code, I open a text file. The text file has 160k lines. I did some operations on each line and concatenated each line together to a very long string. Each line was separated by "\n". I call QTextEdit.setPlainText() to set the very long string into the QTextEdit.
      It is very slow when scrolling the scroll bar or even got "not responding" eventually.

      Could you please give me any suggestions to speed it up or solve the problem?

      Thanks a lot!
      Rex

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Rex-Li
      Tested the following code under Ubuntu, Qt 5.1.5.x, PySide2.

      import sys
      from PySide2 import QtCore, QtWidgets
      # from PyQt5 import QtWidgets
      
      def window():
         app = QtWidgets.QApplication(sys.argv)
         w = QtWidgets.QPlainTextEdit()
         w.resize(800, 800)
         f = QtCore.QFile("plaintextfile.txt")
         f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
         bs = bytes(f.readAll())
         txt = bs.decode('utf-8')
         w.setPlainText(txt)
         f.close()
         w.show()
         sys.exit(app.exec_())
      	
      if __name__ == '__main__':
         window()
      

      File is 180k lines, 6.5MB size. Reads, displays and scrolls immediately and perfectly.

      Suggest you start by removing your own processing and checking above with your original 160k file? (If you have done something wrong about decoding/newlines you might have the whole thing as a single line/bytes, that might be an issue....

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rex Li
        wrote on last edited by
        #3

        @JonB
        Thanks for your reply!
        I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.

        In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
        Then it has the same symptom.
        It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
        If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.

        Did I use the QMainWindow in the wrong way?

        Thanks a lot!
        The following is my code.

        import sys
        from PySide2 import QtCore, QtWidgets
        
        
        # from PyQt5 import QtWidgets
        
        def window():
            app = QtWidgets.QApplication(sys.argv)
            win = MyWindow()
            win.show()
            sys.exit(app.exec_())
        
        class MyWindow(QtWidgets.QMainWindow):
            def __init__(self,parent=None):
                super().__init__(parent)
                self.te = QtWidgets.QTextEdit()
                self.setCentralWidget(self.te)
                self.read_file()
        
            def read_file(self):
                f = QtCore.QFile("abc.txt")
                f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
                bs = bytes(f.readAll())
                txt = bs.decode('utf-8')
                self.te.setPlainText(txt)
                f.close()
        
        if __name__ == '__main__':
            window()
        
        jsulmJ JonBJ 2 Replies Last reply
        0
        • R Rex Li

          @JonB
          Thanks for your reply!
          I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.

          In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
          Then it has the same symptom.
          It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
          If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.

          Did I use the QMainWindow in the wrong way?

          Thanks a lot!
          The following is my code.

          import sys
          from PySide2 import QtCore, QtWidgets
          
          
          # from PyQt5 import QtWidgets
          
          def window():
              app = QtWidgets.QApplication(sys.argv)
              win = MyWindow()
              win.show()
              sys.exit(app.exec_())
          
          class MyWindow(QtWidgets.QMainWindow):
              def __init__(self,parent=None):
                  super().__init__(parent)
                  self.te = QtWidgets.QTextEdit()
                  self.setCentralWidget(self.te)
                  self.read_file()
          
              def read_file(self):
                  f = QtCore.QFile("abc.txt")
                  f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
                  bs = bytes(f.readAll())
                  txt = bs.decode('utf-8')
                  self.te.setPlainText(txt)
                  f.close()
          
          if __name__ == '__main__':
              window()
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Rex-Li said in [PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function:

          self.te = QtWidgets.QTextEdit()

          @JonB used QPlainTextEdit - did you try that?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          2
          • R Rex Li

            @JonB
            Thanks for your reply!
            I tested your code with my file. It worked perfectly. The content can be shown immediately and the scroll bar can work perfectly.

            In my application, I'm using QMainWindow in it. I created a class with QMainWindow based on your code.
            Then it has the same symptom.
            It shows the content immediately, but after I scrolled the scroll bar, it got stuck and then not responding.
            If I don't scroll the scroll bar, it can work fine after nearly 4 minutes.

            Did I use the QMainWindow in the wrong way?

            Thanks a lot!
            The following is my code.

            import sys
            from PySide2 import QtCore, QtWidgets
            
            
            # from PyQt5 import QtWidgets
            
            def window():
                app = QtWidgets.QApplication(sys.argv)
                win = MyWindow()
                win.show()
                sys.exit(app.exec_())
            
            class MyWindow(QtWidgets.QMainWindow):
                def __init__(self,parent=None):
                    super().__init__(parent)
                    self.te = QtWidgets.QTextEdit()
                    self.setCentralWidget(self.te)
                    self.read_file()
            
                def read_file(self):
                    f = QtCore.QFile("abc.txt")
                    f.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
                    bs = bytes(f.readAll())
                    txt = bs.decode('utf-8')
                    self.te.setPlainText(txt)
                    f.close()
            
            if __name__ == '__main__':
                window()
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Rex-Li
            Without even rewriting my code to use QMainWindow, if you try it with QTextEdit instead of QPlainTextEdit then yes scrolling etc. gets very "sticky". I had not noticed you had written "QTextEdit with QTextEdit.setPlainText() function". Assuming your text is plain text, looks like you need to use QPlainTextEdit for speed.

            R 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Rex-Li said in [PySide2]Get "Not Responding" after setting long text to the QTextEdit with QTextEdit.setPlainText() function:

              self.te = QtWidgets.QTextEdit()

              @JonB used QPlainTextEdit - did you try that?

              R Offline
              R Offline
              Rex Li
              wrote on last edited by
              #6

              @jsulm

              Thanks for pointing this out!
              QPlainTextEdit works perfectly for me.

              1 Reply Last reply
              0
              • JonBJ JonB

                @Rex-Li
                Without even rewriting my code to use QMainWindow, if you try it with QTextEdit instead of QPlainTextEdit then yes scrolling etc. gets very "sticky". I had not noticed you had written "QTextEdit with QTextEdit.setPlainText() function". Assuming your text is plain text, looks like you need to use QPlainTextEdit for speed.

                R Offline
                R Offline
                Rex Li
                wrote on last edited by
                #7

                @JonB

                I tried QPlainTextEdit. It works perfectly now.

                Thanks for offering help!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved