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:
- First, we import the GUI library:
from PyQt4.QtGui import *
- Next, we initialize the warning dialog:
msg = QMessageBox()
- Then, we set the warning message:
msg.setText("This is a warning...")
- Now, add a warning icon to the dialog that has an enumeration index of
2
:msg.setIcon(QMessageBox.Warning)
- 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...