QFileDialog
QFileDialog
provides a dialog that allows users to select files or directories.
Methods
Examples
from PySide6 import QtWidgets
from PySide6.QtCore import Slot
app = QtWidgets.QApplication()
window = QtWidgets.QMainWindow()
@Slot()
def save_file():
# Get the filename selected by the user
file_name, _ = QtWidgets.QFileDialog.getSaveFileName(filter="CSV files (*.csv)")
# Save the data to this file
with open(file_name, "w") as file:
file.write(data)
data = "1,2,3,4,5"
button = QtWidgets.QPushButton("Save File")
# A main window always requires a central widget
window.setCentralWidget(button)
button.clicked.connect(save_file)
window.show()
app.exec()
"1,2,3,4,5"
) to a CSV file.
getSaveFileName(caption=None, dir=None, filter=None)
staticmethod
Opens a dialog for saving a file. The dialog is displayed as a modal dialog and returns the selected file name and filter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
caption |
str
|
The dialog caption. |
None
|
dir |
str
|
The initial directory. |
None
|
filter |
str
|
The file filter. Generally, this is a string like "Image files (.png .jpg)" or "Text files (.txt);;CSV files (.csv)", where multiple filters are separated by two semicolons. |
None
|
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
A tuple containing the selected file name and filter. |