dayjournal memo

Total 975 articles!!

QGIS #008 - プラグインでダイアログ作成

Yasunori Kirimoto's avatar

プラグインで複数条件の処理を指定するためにはダイアログを作成する必要があります。ダイアログを作成するには下記の6ファイルが必要になります。

QGIS_008_10


QGISのプラグインでダイアログを作成するためには下記方法でおこないます。


①ダイアログの作成

まず、QGISでダイアログを作成するためには「Qt」を利用します。以前紹介した「Python #001 – PortablePython」を利用して「Qt」を起動します。


PortablePythonの中の「QtDesigner-Portable」を実行します。

QGIS_008_01


QtDesignerを利用してボタンやラベルを任意に配置します。今回はプロジェクト内にあるレイヤ数を表示するプラグインを作成します。

QGIS_008_02


任意のダイアログを作成したら名前を付けて保存。拡張子が「.ui」のファイルを作成します。今回はファイル名を「Dialog01.ui」とします。

QGIS_008_03


②ダイアログの変換

Qtで作成した「Dialog01.ui」を「Dialog01.py」に変換します。

「PortablePython」で変換しようと試したところ変換に必要な「pyuic4.bat」がフォルダ内に存在しない。。。色々と調べてなんとか「pyuic4.bat」を作成する方法を見つけました。

まず、「pyuic4.bat」を下記内容で「Dialog01.ui」と同じディレクトリに作成します。

※ドライブ名が違う場合は書き換えてください。今回はGドライブとしました。


@"G:\PortablePython\App\python" "G:\PortablePython\App\pyuic.py" %1 %2 %3 %4 %5 %6 %7 %8 %9

次に、コマンドプロンプトを実行 → 「Dialog01.ui」があるディレクトリまで移動 → 「pyuic4 Dialog01.ui -o Dialog01.py」を実行。「Dialog01.py」が作成されます。

QGIS_008_06


しかし、プラグイン作成に非常に参考になる「QGISプログラミング入門」を読んでいたところもっと簡単に変換できることが判明しました。最初から確認しておけばよかった。。。QGISをインストールした際に「OSGeo4W」も入れていると下記方法でも変換可能です。

まず、すべてのプログラム → QGISフォルダ → OSGeo4Wを実行

QGIS_008_04


次に、「Dialog01.ui」があるディレクトリまで移動 → 「pyuic4 -o Dialog01.py Dialog01.ui」を実行。「Dialog01.py」が作成されます。

QGIS_008_05



③プラグインの作成

ここでは主に「dialog.py」を新規作成するのと、「sample.py」の一部を変更します。変換された「Dialog01.py」の記述は変更しないでそのまま利用します。



# -*- coding: utf-8 -*-

#---------------------------------------------------------------------------
#     Name:        Sample Plugin03
#     Purpose:     レイヤ数表示プラグイン
#
#     Author:      dayjournal
#
#     Created:     15/07/2015
#     Copyright:   (c) dayjournal 2015
#---------------------------------------------------------------------------

def classFactory(iface):
from sample import SamplePlugin03
return SamplePlugin03(iface)



# -*- coding: utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

import os

class SamplePlugin03:

    def __init__(self, iface):
        self.iface = iface

    def initGui(self):

        path = os.path.dirname(__file__) + u'\icon.png'

        self.action = QAction(QIcon(path), "Layer Count", self.iface.mainWindow())

        QObject.connect(self.action, SIGNAL("triggered()"), self.run)

        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu("Sample Plugin03", self.action)

    def unload(self):

        self.iface.removePluginMenu("Sample Plugin03", self.action)
        self.iface.removeToolBarIcon(self.action)

    def run(self):

        from dialog import Dialog

        dlg = Dialog(self.iface)
        dlg.show()
        dlg.exec_()

ダイアログを読み込む


from dialog import Dialog

ダイアログを表示


dlg = Dialog(self.iface)
dlg.show()
dlg.exec_()



[general]

name = Sample Plugin03

description = レイヤ数表示プラグイン

about = レイヤ数を表示するプラグインを作成しました。

category = sample

tags = sample,plugin

homepage=https://github.com/

repository=https://github.com/

author = dayjournal

email = dayjournal@gmail.com

version = 1.0

changelog =
    1.0 - 初版作成

icon=icon.png

qgisMinimumVersion = 2.8



# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Dialog01.ui'
#
# Created: Wed Jul 15 20:35:21 2015
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(184, 141)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(40, 80, 101, 41))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(35, 20, 111, 41))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "実行", None))
        self.label.setText(_translate("Dialog", "Layer数の表示", None))



# -*- coding: utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

from Dialog01 import Ui_Dialog

class Dialog(QDialog):

    def __init__(self, iface):
        QDialog.__init__(self)
        self.iface = iface
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        QObject.connect(self.ui.pushButton, SIGNAL("clicked()"), self.PushButton)

    def PushButton(self):

        layerall = str(self.iface.mapCanvas().layerCount())
        self.ui.label.setText(layerall)
        

Qt,QGISの機能を読み込む


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

ダイアログを読み込む


from Dialog01 import Ui_Dialog

クラスを宣言


class Dialog(QDialog):

QGISのインターフェイスを読み込む


def __init__(self, iface):
    QDialog.__init__(self)
    self.iface = iface
    self.ui = Ui_Dialog()
    self.ui.setupUi(self)
    

実行ボタンをクリックでdef PushButton(self):を実行


    QObject.connect(self.ui.pushButton, SIGNAL("clicked()"), self.PushButton)
    

実行ボタンを押した時


def PushButton(self):

ラベルにプロジェクトのレイヤ数を表示


    layerall = str(self.iface.mapCanvas().layerCount())
    self.ui.label.setText(layerall)


プラグインを「プラグイン管理」で利用可にすると下記のようにプラグインを実行できるようになります。

QGIS_008_07


プラグインを実行するとダイアログが表示されます。

QGIS_008_08


実行ボタンを押すと、プロジェクト内の全レイヤ数がラベルに表示されます。

QGIS_008_09


ダイアログ設定が含まれてくると、全体の繋がりや仕組みがいまいちピンとこない部分があります。QtとPythonについて詳しく勉強する必要がありそうです。



book

Q&A


QGISプログラミング入門