PyQt - how to make getOpenFileName remember the last opening path?

According to getOpenFileName instructions:

QString fileName = QFileDialog.getOpenFileName(this, tr("Open File"), "/home", tr("Images (*.png *.xpm *.jpg)")); 

How can I make a dialogue remember the path the last time I close it?

and what does tr mean in tr("Open File") ?

thanks

+7
python pyqt qfiledialog
source share
1 answer

If you omit the dir argument (or pass an empty string), the dialog should remember the last directory:

 filename = QtGui.QFileDialog.getOpenFileName( parent, 'Open File', '', 'Images (*.png *.xpm *.jpg)') 

The tr function is used to translate user-visible strings. You can omit this if you will never provide translations for your application.

EDIT

It seems that the start directory cannot be automatically remembered on all platforms / desktop computers, depending on whether you use the native dialog or not. If the built-in Qt dialog is used, the startup directory should always be automatically remembered on all platforms (even between application calls). To try a non-native dialogue, follow these steps:

 filename = QtGui.QFileDialog.getOpenFileName( parent, 'Open File', '', 'Images (*.png *.xpm *.jpg)', None, QtGui.QFileDialog.DontUseNativeDialog) 

Alternatively, you can use the QFileDialog constructor , which will always create a non-native dialog:

 dialog = QtGui.QFileDialog(parent) dialog.setWindowTitle('Open File') dialog.setNameFilter('Images (*.png *.xpm *.jpg)') dialog.setFileMode(QtGui.QFileDialog.ExistingFile) if dialog.exec_() == QtGui.QDialog.Accepted: filename = dialog.selectedFiles()[0] 
+12
source share

All Articles