
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QFileDialog Widget
This widget is a file selector dialog. It enables the user to navigate through the file system and select a file to open or save. The dialog is invoked either through static functions or by calling exec_() function on the dialog object.
Static functions of QFileDialog class (getOpenFileName() and getSaveFileName()) call the native file dialog of the current operating system.
A file filter can also applied to display only files of the specified extensions. The starting directory and default file name can also be set.
Methods used in QFileDialog
Important methods and enumerations of QFileDialog class are listed in the following table −
Sr.No. | Methods & Description |
---|---|
1 |
getOpenFileName() Returns name of the file selected by the user to open it |
2 |
getSaveFileName() Uses the file name selected by the user to save the file |
3 |
setacceptMode() Determines whether the file box acts as open or save dialog QFileDialog.AcceptOpen QFileDialog.AcceptSave |
4 |
setFileMode() Type of selectable files. Enumerated constants are − QFileDialog.AnyFile QFileDialog.ExistingFile QFileDialog.Directory QFileDialog.ExistingFiles |
5 |
setFilter() Displays only those files having mentioned extensions |
Let's start by creating a simple PyQt application with a QFileDialog to open files.
Creating a QFileDialog
In the below example we will create a QMainWindow subclass called FileDialogExample. As we delve into the initUI function, our primary focus is establishing the main window and incorporating a QPushButton. Upon clicking this button, it activates the openFileDialog function. Within this function, we initiate a QFileDialog object and configure its attributes accordingly.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog class FileDialogExample(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("QFileDialog Example") self.setGeometry(100, 100, 400, 300) self.button = QPushButton("Open File", self) self.button.clicked.connect(self.openFileDialog) self.button.setGeometry(150, 150, 100, 30) def openFileDialog(self): file_dialog = QFileDialog(self) file_dialog.setWindowTitle("Open File") file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile) file_dialog.setViewMode(QFileDialog.ViewMode.Detail) if file_dialog.exec(): selected_files = file_dialog.selectedFiles() print("Selected File:", selected_files[0]) def main(): app = QApplication(sys.argv) window = FileDialogExample() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()
Output
The above code produces the following result −

Example 1: Saving Files
In the below example, the QFileDialog is used to ask the user to select a location for saving a file. When the "Save File" button is clicked, a file dialog window appears that enables the user to choose a location and enter a file name for saving. The selected file path for saving is then printed to the console.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog class FileDialogExample(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("QFileDialog Example - Save File") self.setGeometry(100, 100, 400, 300) self.button = QPushButton("Save File", self) self.button.clicked.connect(self.saveFileDialog) self.button.setGeometry(150, 150, 100, 30) def saveFileDialog(self): file_dialog = QFileDialog(self) file_dialog.setWindowTitle("Save File") file_dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave) file_dialog.setViewMode(QFileDialog.ViewMode.Detail) if file_dialog.exec(): selected_file = file_dialog.selectedFiles()[0] print("Selected File for Saving:", selected_file) def main(): app = QApplication(sys.argv) window = FileDialogExample() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()
Output
The above code produces the following result −
Example 2: Selecting Directories
In this example we show how QFileDialog can be used to select directories. When the Select Directory button is clicked then a dialog window opens up that allows the user to browse and choose a directory. The path of the selected directory is then printed to the console.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog class FileDialogExample(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("QFileDialog Example - Select Directory") self.setGeometry(100, 100, 400, 300) self.button = QPushButton("Select Directory", self) self.button.clicked.connect(self.selectDirectoryDialog) self.button.setGeometry(150, 150, 150, 30) def selectDirectoryDialog(self): file_dialog = QFileDialog(self) file_dialog.setWindowTitle("Select Directory") file_dialog.setFileMode(QFileDialog.FileMode.Directory) file_dialog.setViewMode(QFileDialog.ViewMode.List) if file_dialog.exec(): selected_directory = file_dialog.selectedFiles()[0] print("Selected Directory:", selected_directory) def main(): app = QApplication(sys.argv) window = FileDialogExample() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()
Output
The above code produces the following result −
