Docs in progress for 'QGIS testing'. Visit https://docs.qgis.org/3.4 for QGIS 3.4 docs and translations.
本节展示了一些应该用来与用户通信的方法和元素,以保持用户界面的一致性。
从用户体验的角度来看,使用消息框可能是一个坏主意。对于显示小信息行或警告/错误消息,QGIS消息栏通常是更好的选择。
使用对qgis接口对象的引用,可以在消息栏中用以下代码显示消息
from qgis.core import Qgis
iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that", level=Qgis.Critical)
您可以设置一个持续时间以在有限的时间内显示它
iface.messageBar().pushMessage("Ooops", "The plugin is not working as it should", level=Qgis.Critical, duration=3)
上面的例子显示了一个错误条,但是 level
参数可用于创建警告消息或信息消息,使用 Qgis.MessageLevel
枚举。您最多可以使用4个不同级别:
小部件可以添加到消息栏,例如一个按钮来显示更多信息
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)
您甚至可以在自己的对话框中使用消息栏,这样就不必显示消息框,或者如果在主qgis窗口中显示消息框没有意义的话。
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()
进度条也可以放在QGIS消息栏中,因为正如我们所看到的,它接受小部件。下面是一个您可以在控制台中尝试的示例。
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()
此外,您还可以使用内置状态栏报告进度,如下例所示:
vlayer = QgsProject.instance().mapLayersByName("countries")[0]
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()
您可以使用QGIS日志记录系统来记录您想要保存的关于代码执行的所有信息。
# 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)
警告
巨蟒的使用 print
语句在任何可能是多线程的代码中都不安全。这包括 表达式函数, 渲染器, symbol layers 和 Processing algorithms (除其他外)。在这些情况下,您应该始终使用线程安全类( QgsLogger
或 QgsMessageLog
相反。
注解
您可以看到 QgsMessageLog
在 日志消息面板
注解
QgsLogger
is for messages for debugging /
developers (i.e. you suspect they are triggered by some broken code)QgsMessageLog
is for messages to
investigate issues by sysadmins (e.g. to help a sysadmin to fix configurations)