iT邦幫忙

1

Python兩個class相互呼叫

  • 分享至 

  • xImage

最近接觸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文字的物件
https://ithelp.ithome.com.tw/upload/images/20221226/201483532SjaEeFUeV.jpg

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)

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2022-12-26 09:51:11 檢舉
沒寫過PyQGIS,但你的code很怪...
1. 這兩個class互相引用?
2. 看起來iface應該是要帶入 MyMapTool,透過iface去存取

這個建議把你從哪裡找出來的code給出來
win895564 iT邦新手 1 級 ‧ 2022-12-26 13:55:25 檢舉
這code是我自己寫的
FreehandCustomer是QGIS plugin UI的預設class 框架 會自動產生
MyMapTool的框架也是官網提供的
可能我說的不清楚
因為qt上的物件似乎都是在FreehandCustomerDialog()底下
所以我在FreehandCustomer直接輸入self.dlg.物件 就可以做對應的動作
我希望做的事點選某個pushbutton 會呼叫MyMapTool
他可以在QGIS的圖層上移動滑鼠會取得經緯度
然後我要將這個經緯度顯示在另一個物件lineEdit上面
但是沒報錯也沒動作
method也確定沒錯 因為lineEdit要設定文字就是setText
froce iT邦大師 1 級 ‧ 2022-12-26 15:21:32 檢舉
所以說你移動滑鼠會print?

加個 self.freehand.dlg.show() 看看?
win895564 iT邦新手 1 級 ‧ 2022-12-26 15:47:12 檢舉
應該說我兩個class分開都是可以動作的
只是因為我這次打算做plugin所以有UI介面
然後我又希望加入這個功能 才想說把兩個class放一起
我加入self.freehand.dlg.show() 確實有動作
我滑鼠移到QGIS 我的ui確實會有致頂的效果
froce iT邦大師 1 級 ‧ 2022-12-27 08:01:33 檢舉
你的確有觸發canvasMoveEvent,那你該懷疑的是你的FreehandCustomerDialog,而不是你的這兩個class了。
win895564 iT邦新手 1 級 ‧ 2022-12-27 08:35:46 檢舉
對 他預設會
from .freehand_customer_dialog import FreehandCustomerDialog
我再補充一下
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答