1

Name: PyQt6
Version: 6.1.0

OS: Ubuntu 20.04.5 LTS

from PyQt6.QtWidgets import QFileDialog

HOME_PATH = os.getenv("HOME")
...

dir_path = QFileDialog.getExistingDirectory(
    parent=self,
    caption="Select directory",
    directory=HOME_PATH,
    options=QFileDialog.Option.ShowDirsOnly,
)

directory and options do not work.
Init path is wrong.
Files get displayed, i can not select a directory.

Directory only fail

luku
  • 63
  • 5
  • 1
    PyQt6 has introduced a [change in Enum usage](https://www.riverbankcomputing.com/static/Docs/PyQt6/pyqt5_differences.html), which now all require the full namespace. Change to `QFileDialog.Option.ShowDirsOnly`. – musicamante Nov 24 '22 at 08:56
  • @musicamante thanks! This resolves the error. ShowDirsOnly still does not work tho, same as the directory path... meaning i do not start at the path i specified and viewing dirs only fails, meaning i end up selecting a file... – luku Nov 24 '22 at 09:01
  • We don't know what `HOME_PATH` is, so we cannot know if that's a valid path or not. Also, `getExistingDirectory` already shows directories only, try removing that option, as it's not necessary. Finally, specify what Qt version (`QtCore.QT_VERSION_STR`) and OS you're using. – musicamante Nov 24 '22 at 09:10
  • @musicamante i updated the description. "getExistingDirectory already shows directories only" is not the case for me. – luku Nov 24 '22 at 09:19
  • 1
    That seems a bug related to the native Gnome file dialog (as reported in [QTBUG-88709](https://bugreports.qt.io/browse/QTBUG-88709); I can't see further comments for newer Ubuntu/Gnome versions, which *might* mean that it was solved by recent version of those environments. As a workaround, use the Qt dialog with the `QFileDialog.Option.DontUseNativeDialog` option. – musicamante Nov 24 '22 at 09:33
  • @musicamante Perfect! Thanks for the quick and accurate answer explaining the problem and providing the solution. – luku Nov 24 '22 at 09:38

1 Answers1

1

The original question was mainly related to the new way PyQt6 uses Enums, which now always require the full namespace: until PyQt5, the syntax Class.FlagName was sufficient, but PyQt6 now requires Class.EnumName.FlagName.

The other issue is probably related to QTBUG-88709 and is part of a long series of issues that deal with native dialogs provided by the OS, which always depend on the system.

There is no direct solution for this, and the only safe way is to enforce the DontUseNativeDialog flag whenever in doubt:

dir_path = QFileDialog.getExistingDirectory(
    parent=self,
    caption="Select directory",
    directory=HOME_PATH,
    options=QFileDialog.Option.DontUseNativeDialog,
)

Note that:

  1. the ShowDirsOnly is useless as it's always set whenever the file dialog is in Directory mode (which is automatically done for the getExistingDirectory() static function);
  2. using the non-native dialog will obviously be inconsistent with the normal dialog shown by the OS;
  3. it is unclear if the bug was automatically resolved in Ubuntu/Gnome versions following the environment normally used in Ubuntu 20.04;
  4. there is no direct way to enforce the workaround only when required: either you find out the specific cause of the issue, or you just assume it doesn't work and always use the non-native dialog;
musicamante
  • 41,230
  • 6
  • 33
  • 58