Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a warning dialog


Sometimes, you need to notify a user when an issue is detected, which might lead to problems if the user continues. This situation calls for a warning dialog, which we will demonstrate in this recipe.

Getting ready

Open the QGIS Python Console by going to the Plugins menu and selecting Python Console.

How to do it...

In this recipe, we will create a dialog, set the warning message and a warning icon, and display the dialog, as follows:

  1. First, we import the GUI library:

    from PyQt4.QtGui import *
    
  2. Next, we initialize the warning dialog:

    msg = QMessageBox()
    
  3. Then, we set the warning message:

    msg.setText("This is a warning...")
    
  4. Now, add a warning icon to the dialog that has an enumeration index of 2:

    msg.setIcon(QMessageBox.Warning)
    
  5. Finally, we call the execution method to display the dialog:

    msg.show()
    

How it works...

Message dialogs should be used sparingly because they interrupt the user experience and can easily become annoying. However, sometimes it is important to prevent...