3

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.

asked May 20, 2014 at 2:14

1 Answer 1

4

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 ) )
answered May 20, 2014 at 5:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.