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 −

pyqt qfiledialog widget create

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 −

pyqt qfiledialog example 1

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 −

pyqt qfiledialog example 2
Advertisements