【问题标题】:Execute method not blocked in qgis pluginqgis插件中未阻止执行方法
【发布时间】:2021-04-04 16:26:39
【问题描述】:

我正在开发一个 QGIS 插件,这是一个复杂的插件,带有各种类和调用其他方法的方法。 我的插件工作正常,但我希望在后台运行一些类方法(类似于 JAVA 中的作业)以避免冻结 GUI。我的意思是,我有一个类,这个类有一个方法,我想在后台运行这个方法

我尝试了 QThread、QTask、threading、QProcess 等,但我没有找到我想做的事情。

一些想法可以帮助我吗?某些插件确实适用于后台进程?一些插件示例?

谢谢

【问题讨论】:

    标签: python plugins background qgis


    【解决方案1】:

    如果我理解正确,您想刷新 UI 以避免 QGIS 冻结。 这对我有用:

    from PyQt5.QtWidgets import QApplication
    
    QApplication.processEvents() 
    

    这将刷新 QGIS 的 UI。 您可能必须多次调用它,例如在 for 循环中的每次迭代之后。

    【讨论】:

    • 我要做的是在后台运行一个方法而不冻结 QGIS GUI 我的代码示例 def __init__(self): code self.createBotton() def createButton(self): Here I create my button self.okbutton.clicked.connect(self.pressOkButton) def pressOkButton(self): 这里我调用其他方法 self.methodA() self.methodB() def methodA(self): code def methodB(self): code my question is--> 如何在后台运行“pressOkButton”方法?当我执行此方法时,我无法与 QGIS GUI 交互,直到完成方法
    【解决方案2】:

    谢谢你的回答

    我要做的是在后台运行一个方法而不冻结 QGIS GUI 我的代码示例

    class Example:
      def __init__(self):
          code
          self.createBotton()
    
      def createButton(self):
          Here I create my button
          self.okbutton.clicked.connect(self.pressOkButton)
    
      def pressOkButton(self):
          here I call other methods
          self.methodA()
          self.methodB()
    
      def methodA(self):
          code
      def methodB(self):
          code
    

    我的问题是--> 如何在后台运行“pressOkButton”方法?当我执行此方法时,我无法与 QGIS GUI 交互,直到完成方法

    注意:认为它是属于我的插件的一个类,所以这个类在启动插件时被实例化

    【讨论】:

      【解决方案3】:

      我一直在使用 QTask 来在后台运行繁重的进程。 我正在使用的有用资源是这些:

      您必须专门创建一个继承自 QgsTask 的新类,并在 GUI 插件的 run() 方法中调用它。

      就我而言,这就是我所拥有的:

      class MyPlugin:
      """QGIS Plugin Implementation.
          ... __init__ method, tr method, initGui method, ...
      """
          def run(self):
              """Run method that performs the work after clicking 'OK' in the Plugin GUI"""
              task = MyTask('UserFriendlyTaskName', **props)
              QgsApplication.taskManager().addTask( task )
      
      
      class MyTask(QgsTask):
          def __init__(self, task_name, **props):
              QgsTask.__init__(self, desc)
              # do w/e with the props
          def run(self):
              # call your heavy processes
      

      QGIS3 中的 UI 渲染见下面这个 .gif,没有冻结,我仍然可以在插件运行时在地图上移动和编辑数据。

      QGIS GUI Launch Background Process GIF

      希望这个答案能及时找到你!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-27
        • 2018-05-17
        • 1970-01-01
        • 2020-02-26
        • 1970-01-01
        • 2016-06-14
        相关资源
        最近更新 更多