Reputation: 41
I need to create a dialog for selecting a log directory that allows either the selection of a directory or the creation of a directory if a name is typed in that doesn't exist. I have been able to accomplish one or the other but not both at once.
With Accept mode QFileDialog.AcceptOpen
, I can open directories.
With Accept mode QFileDialog.AcceptSave
, I can create directories, but I cannot open an existing directory.
Code Snippet:
def open_or_create_directory(self, log_dir):
"""
open or create a directory
Doesn't work!!!
"""
dialog = QtWidgets.QFileDialog(self, caption='Data Log File Dir')
dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
dialog.setDirectory(log_dir)
dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
dialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly, True)
dialog.setLabelText(QtWidgets.QFileDialog.Accept, "Select")
if dialog.exec_() == QtWidgets.QFileDialog.Accepted:
logdir = dialog.selectedFiles()
The closest path I have been able to find is QFileDialog: Selecting directories and files but I don't know how to implement this in python.
Upvotes: 0
Views: 2715
Reputation: 244282
Surely what the OP points out depends on the native dialogue of the OS since in my desktop manager (plasma-KDE) it allows me to do both tasks, for example with one click it allows me to navigate in a directory and with double-click it allows the editing of the route, and with right click opens a menu where one of the options allows creating directories.
If the problem is with the native dialogue then you can use the custom Qt dialog to do so enable the option QFileDialog :: DontUseNativeDialog, in it you can navigate with a click and by right clicking a menu will open where one of the actions allows the option of create a new folder
from PyQt5 import QtWidgets
def open_or_create_directory(log_dir):
"""
open or create a directory
"""
dialog = QtWidgets.QFileDialog(None, caption='Data Log File Dir')
dialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
dialog.setDirectory(log_dir)
dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
dialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly, True)
dialog.setLabelText(QtWidgets.QFileDialog.Accept, "Select")
if dialog.exec_() == QtWidgets.QFileDialog.Accepted:
logdir = dialog.selectedFiles()
print(logdir)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
open_or_create_directory("/path/of/directory")
If you consider the solution of this link to be correct then it is easy to translate it into python:
dialog = QtWidgets.QFileDialog(self)
dialog.setFileMode(QtWidgets.QFileDialog.Directory)
dialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
l = dialog.findChild(QtWidgets.QListView, "listView")
if l is not None:
l.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
t = dialog.findChild(QtWidgets.QTreeView)
if t is not None:
t.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
nMode = dialog.exec_()
names = dialog.selectedFiles()
Upvotes: 1