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 a user from taking an action that may cause data corruption or a program to crash.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset