Inner widget doesn't closes in a modified QFileDialog
-
I am trying to get QFIleDialog to return folders along with files. I have used a code from Stack Overflow example and with some help from this thread it is in usable state. But, when drop box is opened and Enter clicked, drop box stays on the screen. Here is a complete code:
from PySide2.QtWidgets import (QFileDialog, QApplication, QDialog, QStackedWidget, QListView, QLineEdit, QMessageBox, QWidget, QPushButton, QVBoxLayout) import sys from PySide2.QtCore import QFileInfo class Widget(QWidget): def __init__(self): super().__init__(None) self.button = QPushButton("Open") self.button.clicked.connect(self.onButtonCLicked) self.mainLayout = QVBoxLayout() self.mainLayout.addWidget(self.button) self.setLayout(self.mainLayout) self.resize(300, 100) def onButtonCLicked(self): openFilesAndDirs(parent=self, caption="Hi") 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())) lineEdit = dialog.findChild(QLineEdit) lineEdit.blockSignals(True) lineEdit.setText(' '.join(selected) if selected else '') lineEdit.blockSignals(False) 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) w = Widget() w.show() sys.exit(app.exec_())
When code is executed, when one letter is typed in the edit box popup box appears under the edit widget, and with Down Arrow, one of the entries is selected, it looks like this:
Pressing on Enter key selects and returns selected entry, but drop box stays on the screen:
What could I do here to remove this widget?
It does go away as soon as you click elsewhere, btw.
-
-
@MrAWD said in Inner widget doesn't closes in a modified QFileDialog:
What could I do here to remove this widget?
I assume this is some part of your
dialogItemView
because you obviously use a modifiedQLineEdit
there...
Are these all childs of your dialog? If not, they remain open in position when you close the dialog.
They should close/hide together with it