For example, when I add a vector layer, next time that I want to add another layer it opens up where I chose my last layer from. How would one implement this in Python and/or PyQgis? I'm writing a plugin, and I want user to select layers in the same way as above.

1 Answer 1


You could do it using a combination of QSettings and a custom file-dialog.
To the QFileDialog.getOpenFileNames() dialog a base directory can be passed. So you just need to configure a filter (Only shapefiles) and a base-directory. To get the last-used ones you can just query (lastUsedDir()) them from a QSettingsFile using the functions below.
After file(s) have been selected, just query the basename of those files ( os.path.basename) and call setLastUsedDir() to set this value as attribute.

def lastUsedDir():
  settings = QSettings( "Your PluginName", "short-name" )
  return settings.value( "lastUsedDir", str( "" ) )

def setLastUsedDir( lastDir ):
  path = QFileInfo( lastDir ).absolutePath()
  settings = QSettings( "Your PluginName", "short-name" )
  settings.setValue( "lastUsedDir", str( path ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.