Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
QGIS Python Programming Cookbook, Second Edition

You're reading from   QGIS Python Programming Cookbook, Second Edition Automating geospatial development

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787124837
Length 464 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joel Lawhead Joel Lawhead
Author Profile Icon Joel Lawhead
Joel Lawhead
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Automating QGIS 2. Querying Vector Data FREE CHAPTER 3. Editing Vector Data 4. Using Raster Data 5. Creating Dynamic Maps 6. Composing Static Maps 7. Interacting with the User 8. QGIS Workflows 9. Other Tips and Tricks

Storing and reading project preferences

The QGIS application settings are stored using the Qt API. However, QGIS project settings have their own object. In this recipe, we'll set and read the project title, and then set and read a custom preference for a plugin.

Getting ready

We are going to set a plugin preference using the sample plugin created in an earlier recipe, Creating a QGIS plugin, of this chapter. You can substitute the name of any plugin you want, however. We will also run this recipe in the QGIS Python console for quick testing, but this code will normally be used in a plugin.

How to do it...

In this recipe, we will first write and then read the title of the current project. Then, we will create a custom value for a plugin called splash, which can be used for the plugin startup splash screen if desired. Here are the steps to store and read project preferences:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. In the console, run the following code:
            proj = QgsProject.instance() 
            proj.title("My QGIS Project") 
            proj.title() 
            msg = "Geospatial Python Rocks!" 
            proj.writeEntry("MyPlugin", "splash", msg) 
            proj.readEntry("MyPlugin", "splash", "Welcome!")[0] 
    

How it works...

In the first two lines, we change the title of the current active project and then echo it back. In the next set of two lines, we set up and read the custom settings for a plugin. Notice that the readEntry() method returns a tuple with the desired text and a boolean, acknowledging that the value is set, so we extract the first index to get the text. The read method also allows the default text in case that property is not set (rather than throw an exception, which must be handled), as well as the boolean value False to inform you that the default text was used because the property was not set. The values you set using this method are stored in the project's XML file when you save it.

There's more...

The Qgs Project object has a number of methods and properties that may be useful. The QGIS API documentation details all of them at http://qgis.org/api/classQgsProject.html.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image