Getting RecursionError: maximum recursion depth exceeded while calling a Python object
-
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 exampleSo, 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!
Fedjaps. if there is a better way to get this open QFileDialog to return name of folders, that would work too!
-
@MrAWD
I'm not sure what exactly is going on here, but.... The line causing the infinite recursion isdialog.findChild(QLineEdit).setText(' '.join(selected))
The code connects
selectionChanged
signal toupdateText()
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 pastePySide2
into the edit widget.A "workaround" is to call
QObject.blockSignals()
around thesetText()
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:
- I type
z
. Thez
appears. - I type Backspace.
- The
z
disappears, but is replaced byPySide2
(remember, that is the directory name of the folder I am exploring). - 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 ofblockSignals()
. Replace your originaldialog.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.
- I type
-
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!
-
-
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!