Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting RecursionError: maximum recursion depth exceeded while calling a Python object
Forum Updated to NodeBB v4.3 + New Features

Getting RecursionError: maximum recursion depth exceeded while calling a Python object

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MrAWD
    wrote on last edited by MrAWD
    #1

    I was trying to get Qt open FileDialog to return directory names if they are selected and not just files. Search found this link:
    Stack Overflow example

    So, I have tried the code and whenever I try to delete something from the File name QLineEdit object, I get recursion error from line 36 - this is that line:

    for index in dialogItemView.selectionModel().selectedRows():
    

    Here is the whole code:

    from PySide2.QtWidgets import QFileDialog, QApplication, QDialog, QStackedWidget, \
        QListView, QLineEdit
    import sys
    from PySide2.QtCore import QFileInfo
    
    
    def openFilesAndDirs(parent=None,
                         caption='',
                         directory='',
                         filters='',
                         initialFilter='',
                         options=None):
    
        dialog = QFileDialog(parent, windowTitle=caption)
        dialog.setFileMode(dialog.ExistingFile)
    
        if options:
            dialog.setOptions(options)
        dialog.setOption(dialog.DontUseNativeDialog, True)
    
        if directory:
            dialog.setDirectory(directory)
    
        if filters:
            dialog.setNameFilter(filters)
            if initialFilter:
                dialog.selectNameFilter(initialFilter)
    
        dialog.accept = lambda: QDialog.accept(dialog)
    
        stackedWidget = dialog.findChild(QStackedWidget)
        dialogItemView = stackedWidget.findChild(QListView)
    
        def updateText():
            selected = []
            for index in dialogItemView.selectionModel().selectedRows():
                selected.append('{}'.format(index.data()))
    
            dialog.findChild(QLineEdit).setText(' '.join(selected))
    
    
        dialogItemView.selectionModel().selectionChanged.connect(updateText)
    
        lineEdit = dialog.findChild(QLineEdit)
        dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
    
        filePath = ""
        fileExt = ""
    
        if dialog.exec_() == QDialog.Accepted:
    
            files = dialog.selectedFiles()
            if len(files) > 0:
                finfo = QFileInfo(files[0])
                filePath = finfo.absoluteFilePath()
                fileExt = finfo.completeSuffix()
    
        return filePath, fileExt
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        openFilesAndDirs(parent=None, caption="Test")
        sys.exit(app.exec_())
    

    Once the dialog is opened, enter anything in the edit box and then press Backspace button.

    What can I do here to avoid this recursion error?

    Thanks a lot!
    Fedja

    ps. if there is a better way to get this open QFileDialog to return name of folders, that would work too!

    JonBJ 1 Reply Last reply
    0
    • M MrAWD

      I was trying to get Qt open FileDialog to return directory names if they are selected and not just files. Search found this link:
      Stack Overflow example

      So, I have tried the code and whenever I try to delete something from the File name QLineEdit object, I get recursion error from line 36 - this is that line:

      for index in dialogItemView.selectionModel().selectedRows():
      

      Here is the whole code:

      from PySide2.QtWidgets import QFileDialog, QApplication, QDialog, QStackedWidget, \
          QListView, QLineEdit
      import sys
      from PySide2.QtCore import QFileInfo
      
      
      def openFilesAndDirs(parent=None,
                           caption='',
                           directory='',
                           filters='',
                           initialFilter='',
                           options=None):
      
          dialog = QFileDialog(parent, windowTitle=caption)
          dialog.setFileMode(dialog.ExistingFile)
      
          if options:
              dialog.setOptions(options)
          dialog.setOption(dialog.DontUseNativeDialog, True)
      
          if directory:
              dialog.setDirectory(directory)
      
          if filters:
              dialog.setNameFilter(filters)
              if initialFilter:
                  dialog.selectNameFilter(initialFilter)
      
          dialog.accept = lambda: QDialog.accept(dialog)
      
          stackedWidget = dialog.findChild(QStackedWidget)
          dialogItemView = stackedWidget.findChild(QListView)
      
          def updateText():
              selected = []
              for index in dialogItemView.selectionModel().selectedRows():
                  selected.append('{}'.format(index.data()))
      
              dialog.findChild(QLineEdit).setText(' '.join(selected))
      
      
          dialogItemView.selectionModel().selectionChanged.connect(updateText)
      
          lineEdit = dialog.findChild(QLineEdit)
          dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
      
          filePath = ""
          fileExt = ""
      
          if dialog.exec_() == QDialog.Accepted:
      
              files = dialog.selectedFiles()
              if len(files) > 0:
                  finfo = QFileInfo(files[0])
                  filePath = finfo.absoluteFilePath()
                  fileExt = finfo.completeSuffix()
      
          return filePath, fileExt
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          openFilesAndDirs(parent=None, caption="Test")
          sys.exit(app.exec_())
      

      Once the dialog is opened, enter anything in the edit box and then press Backspace button.

      What can I do here to avoid this recursion error?

      Thanks a lot!
      Fedja

      ps. if there is a better way to get this open QFileDialog to return name of folders, that would work too!

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

      @MrAWD
      I'm not sure what exactly is going on here, but.... The line causing the infinite recursion is

      dialog.findChild(QLineEdit).setText(' '.join(selected))
      

      The code connects selectionChanged signal to updateText() method. updateText() also sets the text of the line edit. Under whatever circumstances --- to do with emptying out the the selected filename edit widget from the Backspace --- leading to the recursion and the error.

      I am not sure what is going on, but when you try to empty out the edit widget the internal Qt code is trying to copy there the name of the directory you are viewing. For example, I run this in my directory /home/jon/PySide2 and it is trying to paste PySide2 into the edit widget.

      A "workaround" is to call QObject.blockSignals() around the setText() call:

              lineEdit = dialog.findChild(QLineEdit)
              lineEdit.blockSignals(True)
              lineEdit.setText(' '.join(selected))
              lineEdit.blockSignals(False)
      

      This prevents the recursion error.

      The behaviour now runs as follows:

      1. I type z. The z appears.
      2. I type Backspace.
      3. The z disappears, but is replaced by PySide2 (remember, that is the directory name of the folder I am exploring).
      4. The word is not shown selected. But hitting Backspace now deletes the word in one go, and leaves the edit widget empty.

      Is this acceptable? Otherwise you need to investigate further what you can do.

      UPDATE
      I can make the behaviour work seemingly correctly by changing to set edit text to a single space if we would have set to empty. Get rid of blockSignals(). Replace your original dialog.findChild(QLineEdit).setText(' '.join(selected)) by:

              lineEdit = dialog.findChild(QLineEdit)
              lineEdit.setText(' '.join(selected) if selected else ' ')
      

      For whatever reason, setting a space seems to avoid it trying to copy the directory name into the edit, and actually does not really put a space there. I think it now works correctly in all situations.

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @MrAWD
        I'm not sure what exactly is going on here, but.... The line causing the infinite recursion is

        dialog.findChild(QLineEdit).setText(' '.join(selected))
        

        The code connects selectionChanged signal to updateText() method. updateText() also sets the text of the line edit. Under whatever circumstances --- to do with emptying out the the selected filename edit widget from the Backspace --- leading to the recursion and the error.

        I am not sure what is going on, but when you try to empty out the edit widget the internal Qt code is trying to copy there the name of the directory you are viewing. For example, I run this in my directory /home/jon/PySide2 and it is trying to paste PySide2 into the edit widget.

        A "workaround" is to call QObject.blockSignals() around the setText() call:

                lineEdit = dialog.findChild(QLineEdit)
                lineEdit.blockSignals(True)
                lineEdit.setText(' '.join(selected))
                lineEdit.blockSignals(False)
        

        This prevents the recursion error.

        The behaviour now runs as follows:

        1. I type z. The z appears.
        2. I type Backspace.
        3. The z disappears, but is replaced by PySide2 (remember, that is the directory name of the folder I am exploring).
        4. The word is not shown selected. But hitting Backspace now deletes the word in one go, and leaves the edit widget empty.

        Is this acceptable? Otherwise you need to investigate further what you can do.

        UPDATE
        I can make the behaviour work seemingly correctly by changing to set edit text to a single space if we would have set to empty. Get rid of blockSignals(). Replace your original dialog.findChild(QLineEdit).setText(' '.join(selected)) by:

                lineEdit = dialog.findChild(QLineEdit)
                lineEdit.setText(' '.join(selected) if selected else ' ')
        

        For whatever reason, setting a space seems to avoid it trying to copy the directory name into the edit, and actually does not really put a space there. I think it now works correctly in all situations.

        M Offline
        M Offline
        MrAWD
        wrote on last edited by
        #3

        @JonB

        Thanks for help with this! Setting that edit box was a reason for recursions as you said and having signal blocked around that line definitely removed the error. I did try you second option there too, but that didn't work for me without blocking the signal to be emitted.

        Now, it still prints the current folder name once you delete everything, which is not great, but sill an improvement from before!

        If anyone have any idea how to improve on that one, please add it here!

        M 1 Reply Last reply
        0
        • M MrAWD referenced this topic on
        • M MrAWD

          @JonB

          Thanks for help with this! Setting that edit box was a reason for recursions as you said and having signal blocked around that line definitely removed the error. I did try you second option there too, but that didn't work for me without blocking the signal to be emitted.

          Now, it still prints the current folder name once you delete everything, which is not great, but sill an improvement from before!

          If anyone have any idea how to improve on that one, please add it here!

          M Offline
          M Offline
          MrAWD
          wrote on last edited by MrAWD
          #4

          After completing all of the above, now there is a strange error that happens when you select an entry and press Enter. Please see this thread for more description about what is happening now

          If you have any idea what is going on, I would appreciate it!

          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