近期利用VSCode\PyCharm+PyQGIS進(jìn)行插件開發(fā),現(xiàn)將要點(diǎn)總結(jié)一下:
一、VSCode,我沒有配置成功,主要是最后一個階段調(diào)試的時候老是不成功。以后會持續(xù)關(guān)注。但是有幾個要點(diǎn):
1、VSCode+PyQIS開發(fā),智能提示的設(shè)置,見下面的文章。網(wǎng)上找不到相關(guān)的文章的。
寫文章-CSDN創(chuàng)作中心
2、配置文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"port": 5678,
"host": "localhost",
"request": "attach",
"type": "python",
//"processId": "${command:pickProcess}",
"pathMappings": [
{
"localRoot": "${workspaceFolder}", // path to your plugin where you are developing
"remoteRoot": "C:\\Users\\...\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins\\your_plugin" // path to where the QGIS plugin folder lives
}
]
}
]
}
我覺得我應(yīng)該是pathmappings這里設(shè)置的問題。
VSCode要啟動一會(大概是加載插件的緣故吧),智能提示才能開始啟效果。
二、PyCharm,我配置成功了,要注意一下順序:
1、QGIS中要安裝的插件:plugin reloader、plugin builder
2、QGIS中插件的所在目錄,建議把工程直接創(chuàng)建在這里:
C:\Users\用戶名\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
3、PyCharm 解釋程序配置,如果不是lts版本的qgis,配置上有所不同:
?4、服務(wù)配置:
?
3、插件安裝:
1)添加包源:
?
應(yīng)用后再移除,然后刷新,就可以了。一定是應(yīng)用后再移除,然后刷新。?
2)安裝包pydevd-pycharm?,注意版本要與剛才服務(wù)的版本對應(yīng),這里我安裝的是232.8660.197
4、編寫代碼:
可以用QGIS3.32.1安裝時自帶的QTDesinger來設(shè)計(jì)。1)先用plugin builder創(chuàng)建,2)然后用QTDesinger來修改界面。
然后編寫代碼:
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.dlg = MyNameDialog()
self.dlg.button_box.clicked.connect(self.okclick)
self.dlg.button_box.clicked.connect(self.okclick)這句是給界面上的按鍵添加方法。
方法這樣寫:
def okclick(self):
pydevd_pycharm.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True)
aa = "cccc"
print("MyName")
# 獲取當(dāng)前項(xiàng)目
project = QgsProject.instance()
layers = project.mapLayers()
for layer_id, layer in layers.items():
print(layer.name())
注意前面需要導(dǎo)入包:
import pydevd_pycharm
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QIcon
from qgis.core import (
QgsProject,
QgsVectorLayer,
QgsMapLayerType,
)
import pydevd_pycharm pydevd_pycharm.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True) 這兩句是需要的,這樣才能調(diào)試,才能查看變量。
然后打斷點(diǎn),點(diǎn)debug。
?然后在界面上運(yùn)行插件:
修改了代碼后編譯后,一定要運(yùn)行一下reloader(上圖后面的框),然后點(diǎn)擊你的插件。
特別注意:
如果僅僅是編輯一段代碼,那么直接用qt自帶的python編輯器就行。這時好像是不能調(diào)試的。
最好的做法是,創(chuàng)建一個python腳本,用編輯器打開,在pycharm編寫,然后它會同時同步到qgis的編輯器中。在qgis中運(yùn)行看效果,要看變量,直接print(...),這個過程很折磨人哦。文章來源:http://www.zghlxwxcb.cn/news/detail-660466.html
如果要能調(diào)試,就必須要按照上面的步驟編寫插件。文章來源地址http://www.zghlxwxcb.cn/news/detail-660466.html
到了這里,關(guān)于VSCode\PyCharm23.2+PyQGIS插件開發(fā)配置相關(guān)要點(diǎn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!