4

I would like to use options in the static method QFileDialog.getOpenFileName.

For example i would like to set two options: QFileDialog.ExistingFile and QFileDialog.Detail.

I have already read this text: http://pyside.github.io/docs/pyside/PySide/QtGui/QFileDialog.html?highlight=getopenfilename#PySide.QtGui.PySide.QtGui.QFileDialog.getOpenFileName but i don't understand how i can use PySide.QtGui.QFileDialog.Options?

dir = self.sourceDir
filters = "Text files (*.txt);;Images (*.png *.xpm *.jpg)"
selected_filter = "Images (*.png *.xpm *.jpg)"
options = "" # ???
fileObj = QFileDialog.getOpenFileName(self, " File dialog ", dir, filters, selected_filter, options)

If i use

    options  = QFileDialog.DirectoryOnly
    options |= QFileDialog.List

it does not work.

(Windows 7 64 Bit, PyCharm 3.4.1 Pro, Python 3.4.0, PySide 1.2.2)

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Igor
  • 358
  • 3
  • 6
  • 14
  • 1
    See http://qt-project.org/doc/qt-4.8/qfiledialog.html#Option-enum for valid options (to translate from c++ to Python replace `::` with `.`) – three_pineapples Jan 17 '15 at 11:52
  • @three_pineapples Thank you for your Answer! I try to use your recommendation but it does not work (for example: options = QFileDialog.List). – Igor Jan 17 '15 at 12:09
  • That's because `QFileDialog.List` is not part of the `QFileDialog.options` enum. You can only use things in the `QFileDialog.options` enum (read the link carefully again) – three_pineapples Jan 17 '15 at 12:20
  • I know what you mean. QFileDialog::List is a QFileDialog::ViewMode. I would like to set the QFileDialog::FileMode and QFileDialog::ViewMode too. How can i do this? OK, but i try also QFileDialog.ShowDirsOnly - it does not work. I need a small example. – Igor Jan 17 '15 at 12:38

1 Answers1

3

You cannot do this if you are using the static functions with a native file-dialog.

The native file-dialogs do not have the same API as the Qt file-dialog, so you can only set the properties that are available via the static function arguments - which means the caption, title, working-directory, filters, and options.

The static functions more or less match the various file modes:

AnyFile = getSaveFileName
ExistingFile = getOpenFileName
Directory = getExistingDirectory
ExistingFiles = getOpenFileNames

When using the static functions, the ShowDirsOnly option will only work with getExistingDirectory. But on Windows, that will open the native "Browse For Folder" dialog (unless you set the DontUseNativeDialog option), and so the ShowDirsOnly option would be redundant.

There is currently no way to set the ViewMode for a native-dialog when using the static functions, and the same goes for all the other APIs that are specific to QFileDialog.

Long-story short: if you want more control over the file-dialog, use the built-in Qt one - that's what it's there for.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336