最近接觸PyQGIS
我有兩個class
FreehandCustomer是QGIS在製作plugin的預設框架
MyMapTool是我後面加上去的
MyMapTool單獨執行確定是可以動作的
FreehandCustomer似乎要再呼叫一個freehand_customer_dialog才可以對ui上的物件作相對應的動作
目前遇到的瓶頸是
FreehandCustomer底下的Call_maptool可以呼叫MyMapTool下的canvasMoveEvent 也可以正常print
卻無法執行self.freehand.dlg.lineEdit_5.setText("test")也沒有報錯
但是self.freehand.dlg.lineEdit_5.setText("test")寫在FreehandCustomer底下是可以正常動作的
感謝
點選取得地圖座標的按鈕
可以正常執行另一個class的抓經緯度的函式
但卻無法設定圖片下方寫著lineEdit文字的物件
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication,Qt,QItemSelectionModel
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
from qgis.core import QgsProject,QgsVectorFileWriter,QgsFeatureRequest,QgsSymbol,QgsRuleBasedRenderer,QgsSvgMarkerSymbolLayer,QgsMarkerSymbol
from PyQt5.QtWidgets import QApplication,QWidget,QToolBar,QMessageBox
from PyQt5 import QtCore,QtGui
# Initialize Qt resources from file resources.py
from .resources import *
# Import the code for the dialog
from .freehand_customer_dialog import FreehandCustomerDialog
import os.path
from qgis.utils import iface
from qgis.gui import QgsMapTool,QgsMapToolEmitPoint,QgsMapTool,QgsMapToolIdentifyFeature
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from PyQt5.QtGui import QStandardItemModel,QStandardItem,QColor,QDoubleValidator,QTextCursor,QImage,QPixmap
from datetime import datetime
import csv,os
class MyMapTool(QgsMapTool):
def __init__(self, canvas):
# 在這裡初始化 MyMapTool 類
super().__init__(canvas)
self.freehand = FreehandCustomer(self)
def canvasPressEvent(self, event):
# 在這裡實現鼠標按下事件的處理
pass
def canvasMoveEvent(self, event):
# 在這裡實現鼠標移動事件的處理
print(event.mapPoint().toString())
self.freehand.dlg.lineEdit_5.setText("test")
def canvasReleaseEvent(self, event):
# 在這裡實現鼠標鬆開事件的處理
pass
class FreehandCustomer:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'FreehandCustomer_{}.qm'.format(locale))
# 創建 MyMapTool 對象 呼叫另一個class
self.dlg = FreehandCustomerDialog()
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Freehand Customer')
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
def Call_maptool(self):
self.maptool = MyMapTool(iface.mapCanvas())
iface.mapCanvas().setMapTool(self.maptool)
from .freehand_customer_dialog import FreehandCustomerDialog
import os
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'freehand_customer_dialog_base.ui'))
class FreehandCustomerDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(FreehandCustomerDialog, self).__init__(parent)
# Set up the user interface from Designer through FORM_CLASS.
# After self.setupUi() you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)