Skip to content

FileChooserButton

Qt Base Class: QWidget

Signature: QWidget(self, parent: Optional[PySide6.QtWidgets.QWidget] = None, f: PySide6.QtCore.Qt.WindowType = Default(Qt.WindowFlags)) -> None

Base classes

Name Children Inherits
Widget
prettyqt.widgets.widget

⋔ Inheritance diagram

graph TD
  1473367127248["custom_widgets.FileChooserButton"]
  1473293661888["widgets.Widget"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473293661888 --> 1473367127248
  1473293688240 --> 1473293661888
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473290849680 --> 1473293661888
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: Widget

Source code in prettyqt\custom_widgets\editors\filechooserbutton.py
class FileChooserButton(widgets.Widget):
    value_changed = core.Signal(pathlib.Path)

    def __init__(
        self,
        extensions: dict[str, list[str]] | None = None,
        mode: widgets.filedialog.AcceptModeStr = "save",
        file_mode: widgets.filedialog.FileModeStr = "existing_files",
        root: None | str | pathlib.Path = None,
        object_name: str = "file_chooser_button",
        **kwargs: Any,
    ):
        """Initialize FileChooserButton.

        Args:
            extensions: dict allowed extensions
                        form: "'name': ['.ext1', '.ext2']"
            mode: Accept mode ("save" or "load")
            file_mode: File mode ("existing_files", "existing_file", "any_file",
                                  or "directory")
            root: Root path
            object_name: Object name
            kwargs: kwargs passed to parent
        """
        super().__init__(object_name=object_name, **kwargs)
        self._path: pathlib.Path | None = None
        self._extensions = extensions or []
        self.mode = mode
        self.file_mode = file_mode
        self.root = root
        layout = widgets.HBoxLayout(self)
        layout.set_margin(0)
        self.lineedit = widgets.LineEdit()
        self.lineedit.set_read_only()
        layout.add(self.lineedit)
        action = gui.Action(triggered=self.open_file)
        if self.file_mode == "directory":
            action.set_icon("mdi.folder-outline")
        else:
            action.set_icon("mdi.file-outline")

        self.button = widgets.ToolButton()
        self.button.setDefaultAction(action)
        layout.add(self.button)

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, pathlib.Path)

    @core.Slot()
    def open_file(self):
        dialog = widgets.FileDialog(
            parent=self,
            path_id="file_path",
            mode=self.mode,
            path=self.root,
            file_mode=self.file_mode,
        )
        if self._extensions:
            dialog.set_extension_filter(self._extensions)
        if not dialog.choose():
            return
        self.set_path(dialog.selected_file())
        self.value_changed.emit(self._path)

    def set_path(self, path: datatypes.PathType | None):
        if path is None:
            self._path = None
            self.lineedit.set_text("")
        else:
            self._path = pathlib.Path(path)
            self.lineedit.set_text(os.fspath(path))

    def get_value(self) -> pathlib.Path | None:
        return self._path

    def set_value(self, value: datatypes.PathType | None):
        self.set_path(value)

    def get_extensions(self) -> list[str]:
        return self._extensions

    def set_extensions(self, extensions: list[str]):
        self._extensions = extensions

__init__(extensions: dict[str, list[str]] | None = None, mode: widgets.filedialog.AcceptModeStr = 'save', file_mode: widgets.filedialog.FileModeStr = 'existing_files', root: None | str | pathlib.Path = None, object_name: str = 'file_chooser_button', **kwargs: Any)

Initialize FileChooserButton.

Parameters:

Name Type Description Default
extensions dict[str, list[str]] | None

dict allowed extensions form: "'name': ['.ext1', '.ext2']"

None
mode AcceptModeStr

Accept mode ("save" or "load")

'save'
file_mode FileModeStr

File mode ("existing_files", "existing_file", "any_file", or "directory")

'existing_files'
root None | str | Path

Root path

None
object_name str

Object name

'file_chooser_button'
kwargs Any

kwargs passed to parent

{}
Source code in prettyqt\custom_widgets\editors\filechooserbutton.py
def __init__(
    self,
    extensions: dict[str, list[str]] | None = None,
    mode: widgets.filedialog.AcceptModeStr = "save",
    file_mode: widgets.filedialog.FileModeStr = "existing_files",
    root: None | str | pathlib.Path = None,
    object_name: str = "file_chooser_button",
    **kwargs: Any,
):
    """Initialize FileChooserButton.

    Args:
        extensions: dict allowed extensions
                    form: "'name': ['.ext1', '.ext2']"
        mode: Accept mode ("save" or "load")
        file_mode: File mode ("existing_files", "existing_file", "any_file",
                              or "directory")
        root: Root path
        object_name: Object name
        kwargs: kwargs passed to parent
    """
    super().__init__(object_name=object_name, **kwargs)
    self._path: pathlib.Path | None = None
    self._extensions = extensions or []
    self.mode = mode
    self.file_mode = file_mode
    self.root = root
    layout = widgets.HBoxLayout(self)
    layout.set_margin(0)
    self.lineedit = widgets.LineEdit()
    self.lineedit.set_read_only()
    layout.add(self.lineedit)
    action = gui.Action(triggered=self.open_file)
    if self.file_mode == "directory":
        action.set_icon("mdi.folder-outline")
    else:
        action.set_icon("mdi.file-outline")

    self.button = widgets.ToolButton()
    self.button.setDefaultAction(action)
    layout.add(self.button)

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags