[caption id="" align="aligncenter" width="500"] 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[/caption]
[caption id="" align="aligncenter" width="446"] Load plugin vào QGIS[/caption]
[caption id="" align="aligncenter" width="402"] 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[/caption]
Thêm combo box và label
[caption id="" align="aligncenter" width="500"] Thêm combo box và label[/caption]
test thử
[caption id="" align="aligncenter" width="500"] 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[/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++[/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++[/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[/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: Cancel1: 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[/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 …[/caption]Viết hàm hộp thoại mở file
Mở hộp thoại và gán đường dẫn cho lineEdit
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 …[/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”
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! @@