简体   繁体   English

PyQT5 QGIS对话框交互

[英]PyQT5 QGIS Dialog Interactions

Background: Im trying to use a QT designer form as a QGIS attribute form (which is fine and working) but the code for this doesnt follow any documentation or examples ive been able to find. 背景:我试图将QT设计器表单用作QGIS属性表单(这很好并且可以正常工作),但是此代码并未遵循任何我已经能够找到的文档或示例。 I did find a little from 2007 which has at least allowed me to get alot working but i feel like its terribly inefficient. 从2007年开始,我确实发现了一点点信息,这至少使我得以大量工作,但我觉得它效率极低。

Problem: Getting access to events (closeEvent) from a dialog form within QGIS. 问题:从QGIS中的对话框形式访问事件(closeEvent)。

import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):               

        self.setGeometry(300, 300, 250, 150)        
        self.setWindowTitle('Message box')    
        self.show()


    def closeEvent(self, event):

        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes | 
            QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()        


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

I got this code from Zetcode Link which im sure would work for standalone of pyqt5 but if i try use this in qgis it crashes the entire application. 我从Zetcode Link获得了这段代码,我肯定它可以单独用于pyqt5,但是如果我尝试在qgis中使用它,则会崩溃整个应用程序。 I believe this is because QGIS is the application and the dialog is under that somehow so the event maybe links to the dialog differently and im not sure how to access that. 我相信这是因为QGIS是应用程序,而对话框位于该对话框下,因此该事件可能以不同的方式链接到该对话框,并且不确定如何访问该对话框。 Does anyone have an idea of how i could access the close event for the dialog thats created using QGIS attribute forms. 有谁知道我如何使用QGIS属性表单创建的对话框访问close事件。

You can not use PyQt like you would do with a standalone app, take a look at this: developing-python-plugin 您不能像使用独立应用程序那样使用PyQt,请看一下: developing-python-plugin

In your code, you are instantating a new QApplication, where your parent application should be QGis; 在您的代码中,您将实例化一个新的QApplication,其中您的父应用程序应为QGis。 see the QgsInterface class 参见QgsInterface

The pyqgis documentation also recommend the use of the pluginbuilder plugin, which will help you to create a template of your plugin. pyqgis文档还建议使用pluginbuilder插件,这将帮助您创建插件的模板。

Also, be aware that PyQt5 is only supported with qgis 3+. 另外,请注意,qgis 3+仅支持PyQt5。 For prior versions, use PyQt4. 对于以前的版本,请使用PyQt4。

Yes, it is working properly, you just avoid the 是的,它工作正常,您只需避免

if __name__ == '__main__':

section and just instantiate the class 部分并实例化该类

ex = Example()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM