Cette section montre quelques méthodes et éléments qui devraient être employés pour communiquer avec l’utilisateur dans l’objectif de conserver une certaine constance dans l’interface utilisateur
Using mesages boxes can be a bad idea from a user experience point of view. For showing a small info line or a warning/error messages, the QGIS message bar is usually a better option
Using the reference to the QGIS interface object, you can show a message in the message bar with the following code.
iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't \
do that", level=QgsMessageBar.CRITICAL)
You can set a duration to show it for a limited time.
iface.messageBar().pushMessage("Error", ""Ooops, the plugin is not working as \
it should", level=QgsMessageBar.CRITICAL, duration=3)
The examples above show an error bar, but the level parameter can be used to creating warning messages or info messages, using the QgsMessageBar.WARNING and QgsMessageBar.INFO constants repectively.
Des Widgets peuvent être ajoutés à la barre de message comme par exemple un bouton pour montrer davantage d’information
def showError():
pass
widget = iface.messageBar().createMessage("Missing Layers", "Show Me")
button = QPushButton(widget)
button.setText("Show Me")
button.pressed.connect(showError)
widget.layout().addWidget(button)
iface.messageBar().pushWidget(widget, QgsMessageBar.WARNING)
You can even use a message bar in your own dialog so you don’t have to show a message box, or if it doesn’t make sense to show it in the main QGIS window.
class MyDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
self.bar = QgsMessageBar()
self.bar.setSizePolicy( QSizePolicy.Minimum, QSizePolicy.Fixed )
self.setLayout(QGridLayout())
self.layout().setContentsMargins(0,0,0,0)
self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok)
self.buttonbox.accepted.connect(self.run)
self.layout().addWidget(self.buttonbox , 0,0,2,1)
self.layout().addWidget(self.bar, 0,0,1,1)
def run(self):
self.bar.pushMessage("Hello", "World", level=QgsMessageBar.INFO)
Les barres de progression peuvent également être insérées dans la barre de message QGis car, comme nous l’avons déjà vu, cette dernière accepte les widgets. Voici un exemple que vous pouvez utilisez dans la console.
import time
from PyQt4.QtGui import QProgressBar
from PyQt4.QtCore import *
progressMessageBar = iface.messageBar().createMessage("Doing something boring...")
progress = QProgressBar()
progress.setMaximum(10)
progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
progressMessageBar.layout().addWidget(progress)
iface.messageBar().pushWidget(progressMessageBar, iface.messageBar().INFO)
for i in range(10):
time.sleep(1)
progress.setValue(i + 1)
iface.messageBar().clearWidgets()
Also, you can use the built-in status bar to report progress, as in the next example.
count = layers.featureCount() for i, feature in enumerate(features):
#do something time-consuming here ... percent = i / float(count) * 100 iface.mainWindow().statusBar().showMessage(“Processed {} %”.format(int(percent)))
iface.mainWindow().statusBar().clearMessage()
Vous pouvez utiliser le système de journal de QGis pour enregistrer toute information à conserver sur l’exécution de votre code.
QgsMessageLog.logMessage("Your plugin code has been executed correctly", \
QgsMessageLog.INFO)
QgsMessageLog.logMessage("Your plugin code might have some problems", \
QgsMessageLog.WARNING)
QgsMessageLog.logMessage("Your plugin code has crashed!", \
QgsMessageLog.CRITICAL)