传递外部参数保持隐式参数

Passing external parameter keeping implicit parameter

我正在为 qgis 3 编写 python 插件。

基本上,当用户点击某个功能时,我会尝试获取它。

mapTool.featureIdentified.connect(onFeatureIdentified)

所以在函数onfeatureIdentified

def onFeatureIdentified(feature):
        print("feature selected : "+ str(feature.id()))

方法 featureIdentified 传递一个隐式参数。

void QgsMapToolIdentifyFeature::featureIdentified ( const QgsFeature & ) void QgsMapToolIdentifyFeature::featureIdentified ( QgsFeatureId )

我的问题是我想将另一个参数传递给该函数(我想在识别到功能时关闭我的 windows) 像那样:

mapTool.featureIdentified.connect(onFeatureIdentified(window))

def onFeatureIdentified(feature,window):
            print("feature selected : "+ str(feature.id()))
            window.quit()

通过这样做,window 参数会覆盖本机方法的隐式参数。

我该怎么办?

有两种方法可以做到:

  • 使用 lambda 函数(归功于 acw1668)传递第二个参数

    mapTool.featureIdentified.connect(lambda feature: onFeatureIdentified(feature, window))
    

    然后

    def onFeatureIdentified(feature,window):
    
  • 如果你使用class(像我一样):

    在 class 的 __init__ 函数中定义 window 然后总是通过 [=28 引用 window =]self.window

    mapTool.featureIdentified.connect(self.onFeatureIdentified)
    

    然后

    def onFeatureIdentified(self,feature):
    print("feature selected : "+ str(feature.id()))
    self.window.quit()
    

    第一个参数将是 self 然后它将传递函数的本机参数