Note Viết plugin python cho QGIS – phần 1

Table of Contents

Hôm nay mình sẽ note lại quá trình tìm hiểu plugin QGIS (updating this post)

Cài plugin builder

[caption id="" align="aligncenter" width="500"]Cài plugin Builder Cài plugin Builder[/caption]  
Nhập thông tin plugin
[caption id="" align="alignnone" width="500"]Cài plugin Builder Cài plugin Builder[/caption]
[caption id="" align="aligncenter" width="500"]Cài plugin Builder Cài plugin Builder[/caption]
[caption id="" align="aligncenter" width="500"]Cài plugin Builder Cài plugin Builder[/caption]
Lưu plugin vào thư mục
c:\Users\username.qgis2\python\plugins
[caption id="" align="alignnone" width="500"]Cài plugin Builder Cài plugin Builder[/caption]
Compile the resources file using pyrcc4 Mở OSGeo4W
pyrcc4 -o resources.py resources.qrc
pyuic4 -o soiqualang_dialog_base.py soiqualang_dialog_base.ui
[caption id="" align="aligncenter" width="500"]Compile the resources file using pyrcc4 Compile the resources file using pyrcc4[/caption]
Sau khi compile xong ta sẽ có các file python tương ứng Load plugin vào QGIS
[caption id="" align="aligncenter" width="500"]Load plugin vào QGIS Load plugin vào QGIS[/caption]
[caption id="" align="aligncenter" width="446"]Load plugin vào QGIS Load plugin vào QGIS[/caption]
[caption id="" align="aligncenter" width="402"]Load plugin vào QGIS Load plugin vào QGIS[/caption]
Sử dụng QT Creator Mở file *.ui trong thư mục plugin
[caption id="" align="aligncenter" width="500"]Mở file *.ui trong thư mục plugin Mở file *.ui trong thư mục plugin[/caption]
Thêm combo box và label
[caption id="" align="aligncenter" width="500"]Thêm combo box và label Thêm combo box và label[/caption]
test thử
[caption id="" align="aligncenter" width="500"]Thêm combo box và label Thêm combo box và label[/caption]
Một số code Qpython dùng console
from qgis.core import *
import qgis.utils
layer=qgis.utils.iface.activeLayer()
print(layer.featureCount())
print(layer.source())
print(layer.name())
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
   print(lyr.name())
#add layer
layer1=QgsVectorLayer(“C:/Users/dtlong/Desktop/pyqgis_code_data/pyqgis_data/alaska.shp”,“layer_name_you_like”, “ogr”)
QgsMapLayerRegistry.instance().addMapLayer(layer1)
[caption id="" align="aligncenter" width="500"]Một số code Qpython dùng console Một số code Qpython dùng console[/caption]
Lỗi unexpected indent  khi edit python trên Notepad++
[caption id="" align="aligncenter" width="500"]Lỗi unexpected indent khi edit python trên Notepad++ Lỗi unexpected indent khi edit python trên Notepad++[/caption]
Cái này do Notepad++ thêm ký tự lạ khi ta xuống dòng và sử dụng chức năng Auto indent của Notepad++ Giải quyết
[caption id="" align="aligncenter" width="500"]Lỗi unexpected indent khi edit python trên Notepad++ Lỗi unexpected indent khi edit python trên Notepad++[/caption]
Thêm lựa chọn cho combobox
self.dlg.layerCombo.addItem( ‘hahaha’, 'hohoho’ )
Với  layerCombo là tên combobox
Dùng phương thức  addItem Ta sẽ get all layers hiện đang mở, sau đó chạy vòng lập để gán vào combobox
lyrs=QgsMapLayerRegistry.instance().mapLayers().values()
for lyr in lyrs:
     self.dlg.layerCombo.addItem( lyr.name(), lyr )
 
[caption id="" align="aligncenter" width="500"]Thêm lựa chọn cho combobox Thêm lựa chọn cho combobox[/caption]
Gán hành động cho 2 nút OK và Cancel
if result==1:
           # Do something useful here - delete the line containing pass and
           # substitute with your code.
           index=self.dlg.layerCombo.currentIndex()
           layer = self.dlg.layerCombo.itemData(index)
           QMessageBox.information(self.iface.mainWindow(),“hello world”,“%s has %d features.” %(layer.name(),layer.featureCount()))
           pass
if result==0:
           QMessageBox.information(self.iface.mainWindow(),u"Tiêu đề cancel",u'Khang bị khùng’)
 
0: Cancel 1: Ok

Cách gán sự kiện cho 1 thành phần trên giao diện

[caption id="" align="aligncenter" width="500"]Cách gán sự kiện cho 1 thành phần trên giao diện Cách gán sự kiện cho 1 thành phần trên giao diện[/caption]
Ta sẽ gán sự kiện mở hộp thoại chọn file cho nút … Đầu tiên, để có thể sử dụng được hộp thoại mở file và hộp thoại thông báo, ta cần khai báo ở đây
[caption id="" align="aligncenter" width="500"]Ta sẽ gán sự kiện mở hộp thoại chọn file cho nút … Ta sẽ gán sự kiện mở hộp thoại chọn file cho nút …[/caption]
Viết hàm hộp thoại mở file Mở hộp thoại và gán đường dẫn cho lineEdit
def selectoutput(self):
       fname = QFileDialog.getSaveFileName(self.dlg, “Select output file ”,“”, ’*.txt’)
       self.dlg.lineEdit.setText(fname)
 
Gán hàm mở file cho nút … Ta sẽ gán sự kiện onclick cho nút pushButton vào trong hàm add_action
#them su kien click vao nut …
self.dlg.lineEdit.clear()
self.dlg.pushButton.clicked.connect(self.selectoutput)
 
Kết quả:
[caption id="" align="alignnone" width="500"]Gán hàm mở file cho nút … Gán hàm mở file cho nút …[/caption] Hết ngày hôm nay, mai làm tiếp :v soiqualang_chentreu
]]>


Warning: Undefined variable $comments_number in /shared/webdir2/dothanhlong.org/wp-content/themes/business-hub/comments.php on line 35

Warning: Undefined variable $comments_number in /shared/webdir2/dothanhlong.org/wp-content/themes/business-hub/comments.php on line 39
0 thoughts on “Note Viết plugin python cho QGIS – phần 1”

  1. Bài viết rất bổ ích! Mình rất vui khi được copy toàn bộ website này về website mình nếu được sự đồng ý của anh Long Đẹp Trai! @@

Leave a Reply

Your email address will not be published. Required fields are marked *