The code snippets on this page need the following imports if you’re outside the pyqgis console:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from qgis.core import (
QgsMessageLog,
QgsGeometry,
)
from qgis.gui import (
QgsMessageBar,
)
from qgis.PyQt.QtWidgets import (
QSizePolicy,
QPushButton,
QDialog,
QGridLayout,
QDialogButtonBox,
)
|
13. Kullanıcı iletişim arayüzü¶
Bu bölüm kullanıcı ile iletişim kurmamızı sağlayan metod ve bileşenlerin kullanımını içerir.
13.1. Mesajları gösterme. QgsMessageBar sınıfı¶
Mesaj kutuları kullanarak mesajlarımızı göstermek kullanıcı açısından kötü bir deneyim oluşturur. Bunun yerine uyarı ve hata mesajlarını bilgi çubuğu, mesaj satırı bölmesinde göstermek QGIS için daha mantıklı bir yöntemdir. Toplu işlemlerde yüzlerce hata ve uyarı mesajı oluşabilir.
QGIS arayüzünde mesaj satırı kısmını kullanarak mesaj göstermek için kullanılacak kod örneği:
from qgis.core import Qgis
iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that", level=Qgis.Critical)
Messages(2): Error : I'm sorry Dave, I'm afraid I can't do that

Şekil 13.23 QGIS Mesaj çubuğu¶
Mesajların belirli bir süre gösterilip ardından mesaj çubuğunun kaybolmasını sağlamak için süre sınırı koyabilirsiniz.
iface.messageBar().pushMessage("Ooops", "The plugin is not working as it should", level=Qgis.Critical, duration=3)
Messages(2): Ooops : The plugin is not working as it should

Şekil 13.24 QGIS Mesaj çubuğu, zaman sınırlaması ile¶
Yukarıdaki örnekte hata mesajı görülmekte, fakat level
parametresi uyarı veya bilgi mesajı tipi olabilir. Qgis.MessageLevel
seçenekleri kullanılabilir. 4 farklı seviye öntanımlıdır:
Bilgi
Uyarı
Kritik
Başarılı

Şekil 13.25 QGIS Mesaj çubuğu (bilgi)¶
Mesaj çubuğuna araçlar eklenebilir, örneğin daha fazla bilgi göstermek için bir buton
1 2 3 4 5 6 7 8 9 | 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, Qgis.Warning)
|
Messages(1): Missing Layers : Show Me

Şekil 13.26 Butonlu bir QGIS Mesaj çubuğu¶
Bu şekilde mesaj çubuğunu kendi mesaj kutunuzu göstermek için kullanabilirsiniz yada mesaj kutusu göstermek zorunlu değilse kullanılabilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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=Qgis.Info)
myDlg = MyDialog()
myDlg.show()
|

Şekil 13.27 QGIS, özel bileşenli mesaj çubuğu¶
13.2. İlerleme durumu gösterme¶
İlerleme durum çubukları QGIS mesaj çubuğu içinde gösterilebilir. İşte burada konsolda deneyebileceğiniz örnek bir kod.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import time
from qgis.PyQt.QtWidgets import QProgressBar
from qgis.PyQt.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, Qgis.Info)
for i in range(10):
time.sleep(1)
progress.setValue(i + 1)
iface.messageBar().clearWidgets()
|
Messages(0): Doing something boring...
Ayrıca entegre ilerleme durum çubuğunu kullanabilirsiniz. İşte sonraki örnek kod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | vlayer = iface.activeLayer()
count = vlayer.featureCount()
features = vlayer.getFeatures()
for i, feature in enumerate(features):
# do something time-consuming here
print('.') # printing should give enough time to present the progress
percent = i / float(count) * 100
# iface.mainWindow().statusBar().showMessage("Processed {} %".format(int(percent)))
iface.statusBarIface().showMessage("Processed {} %".format(int(percent)))
iface.statusBarIface().clearMessage()
|
13.3. Loglama¶
There are three different types of logging available in QGIS to log and save all the information about the execution of your code. Each has its specific output location. Please consider to use the correct way of logging for your purpose:
QgsMessageLog
is for messages to communicate issues to the user. The output of the QgsMessageLog is shown in the Log Messages Panel.The python built in logging module is for debugging on the level of the QGIS Python API (PyQGIS). It is recommended for Python script developers that need to debug their python code, e.g. feature ids or geometries
QgsLogger
is for messages for QGIS internal debugging / developers (i.e. you suspect something is triggered by some broken code). Messages are only visible with developer versions of QGIS.
Examples for the different logging types are shown in the following sections below.
Uyarı
Use of the Python print
statement is unsafe to do in any code which may be
multithreaded and extremely slows down the algorithm. This includes expression
functions, renderers,
symbol layers and Processing algorithms (amongst others). In these
cases you should always use the python logging module or thread safe classes
(QgsLogger
or QgsMessageLog
) instead.
13.3.1. QgsMessageLog¶
# You can optionally pass a 'tag' and a 'level' parameters
QgsMessageLog.logMessage("Your plugin code has been executed correctly", 'MyPlugin', level=Qgis.Info)
QgsMessageLog.logMessage("Your plugin code might have some problems", level=Qgis.Warning)
QgsMessageLog.logMessage("Your plugin code has crashed!", level=Qgis.Critical)
MyPlugin(0): Your plugin code has been executed correctly
(1): Your plugin code might have some problems
(2): Your plugin code has crashed!
Not
QgsMessageLog
sınıfının çıktılarını Log Messages Panel içerisinde görebilirsiniz.
13.3.2. The python built in logging module¶
1 2 3 4 5 6 | import logging
formatter = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logfilename=r'c:\temp\example.log'
logging.basicConfig(filename=logfilename, level=logging.DEBUG, format=formatter)
logging.info("This logging info text goes into the file")
logging.debug("This logging debug text goes into the file as well")
|
The basicConfig method configures the basic setup of the logging. In the above code the filename, logging level and the format are defined. The filename refers to where to write the logfile to, the logging level defines what levels to output and the format defines the format in which each message is output.
2020-10-08 13:14:42,998 - root - INFO - This logging text goes into the file
2020-10-08 13:14:42,998 - root - DEBUG - This logging debug text goes into the file as well
If you want to erase the log file every time you execute your script you can do something like:
if os.path.isfile(logfilename):
with open(logfilename, 'w') as file:
pass
Further resources on how to use the python logging facility are available at:
Uyarı
Please note that without logging to a file by setting a filename the logging may be multithreaded which heavily slows down the output.