Outdated version of the documentation. Find the latest one here.

사용자에게 정보 전달하기

이 장에서는 사용자 인터페이스에서 일관성을 유지하기 위해 이용해야 하는 몇몇 메소드 및 요소들에 대해 설명합니다.

Showing messages. The QgsMessageBar class

메시지 상자를 이용하는 것은 사용자 경험이라는 관점에서 그리 좋은 생각이 아닙니다. 짧은 정보 또는 경고/오류 메시지를 표출하는 데에는 QGIS 메시지 바를 이용하는 편이 더 낫습니다.

QGIS 인터페이스 오브젝트의 참조(reference)를 사용하면, 다음 코드를 통해 메시지 바에 메시지를 표시할 수 있습니다.

from qgis.gui import QgsMessageBar
iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that", level=QgsMessageBar.CRITICAL)
../../_images/errorbar.png

QGIS 메시지 바

지속 시간(duration)을 설정해서 제한된 시간 동안 메시지를 표출할 수 있습니다.

iface.messageBar().pushMessage("Error", "Ooops, the plugin is not working as it should", level=QgsMessageBar.CRITICAL, duration=3)
../../_images/errorbar-timed.png

타이머를 적용한 QGIS 메시지 바

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 respectively.

../../_images/infobar.png

QGIS 메시지 바 (정보)

추가적인 정보 표시를 위한 버튼 등의 위젯을 메시지 바에 추가할 수도 있습니다.

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)
../../_images/bar-button.png

버튼이 들어간 QGIS 메시지 바

별도의 메시지 상자가 필요 없거나 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=QgsMessageBar.INFO)
../../_images/dialog-with-bar.png

사용자 지정 대화창 내부의 QGIS 메시지 바

진행률 표시

앞에서 배웠듯이 QGIS 메시지 바에 위젯을 추가할 수 있으므로, 진행률(progress) 막대도 추가할 수 있습니다. 다음은 여러분이 콘솔에서 실행해볼 수 있는 예시 코드입니다.

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()

로그 기록

QGIS의 로그 기록 시스템을 이용하면 여러분의 코드 실행에 대해 저장하고자 하는 모든 정보를 로그에 기록할 수 있습니다.

# You can optionally pass a 'tag' and a 'level' parameters
QgsMessageLog.logMessage("Your plugin code has been executed correctly", 'MyPlugin', QgsMessageLog.INFO)
QgsMessageLog.logMessage("Your plugin code might have some problems", level=QgsMessageLog.WARNING)
QgsMessageLog.logMessage("Your plugin code has crashed!", level=QgsMessageLog.CRITICAL)