ユーザとのコミュニケーション

このセクションではユーザインターフェースにおいて継続性を維持するためにユーザとのコミュニケーション時に使うべきメソッドとエレメントをいくつか示しています。

メッセージ表示中。QgsMessageBar クラス。

メッセージボックスの使用はユーザ体験の見地からは良いアイデアではありません。情報、警告/エラー用の小さな表示行には、たいていQGIS メッセージバーが良い選択肢です。

QGIS インタフェースオブジェクトへの参照を利用すると、次のようなコードでメッセージバー内にメッセージを表示することができます。

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

QGIS メッセージバー

表示期間を設定して時間を限定することができます。

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

ターマー付きQGIS メッセージバー

上記の例はエラーバーを示していますが、 level パラメータは QgsMessageBar.WARNING および QgsMessageBar.INFO 定数をそれぞれに使って、警告メッセージやお知らせメッセージを作成するのに使うことができます。

../../_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メッセージバーに置くこともできます。コンソール内で試すことができる例はこちらです。

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

次の例のように、ビルトインステータスバーを使って進捗をレポートすることもできます

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ロギングシステムを使うとコードの実行に関して保存したい情報のログを全て採ることができます。

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)