关闭视窗的表格qgis

时间:2018-10-03 13:52:49

标签: python forms pyqt5 qgis

我正在使用qt设计器和后面的一些python代码为qgis 3创建自定义表单,以进行验证。

但是,我在映射按钮操作“确定”和“取消”时遇到问题。我首先断开按钮,然后将它们连接到其他功能以验证表单中的数据。

但是,当我尝试调用close函数(以关闭窗口的窗体)时,它只会使窗体消失,但窗口仍然存在

enter image description here

这是我的代码:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QLineEdit, QDialogButtonBox, QComboBox, QLabel, QPushButton
import psycopg2

myDialog = None
cbb_implant = None
Validat = 0


def formOpen(dialog,layerid,featureid):

    bdd = psycopg2.connect("host=localhost")
    cursor = bdd.cursor()

    global myDialog
    myDialog = dialog

    dialog.hideButtonBox()

    global cbb_implant
    cbb_implant = dialog.findChild(QComboBox, "cbb_implantation")
    cursor.execute('SELECT * FROM l_implantation_type')
    fetch = cursor.fetchall()
    if len(fetch)>0:
        for i in range(0,len(fetch)) :
            cbb_implant.addItem(fetch[i][1])
        cbb_implant.setCurrentIndex(7)

    ok_chem = dialog.findChild(QPushButton, "ok_chem")

    ok_chem.clicked.connect(validate)


def validate():
    global Validat

    if Validat == 0 : 
        print("HELLO")
        Validat = 1
    else :
        myDialog.save()
        myDialog.close()

谢谢您的帮助

3 个答案:

答案 0 :(得分:0)

我发现了另一种关闭整个窗口的方法。

我只是通过使用pyautogui模块模拟了转义键的按下,它就完成了工作。

答案 1 :(得分:0)

您可以尝试使用此代码

myDialog.parent().close()

这对我有用

答案 2 :(得分:0)

希望这会有所帮助:

https://nathanw.net/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/

from PyQt4.QtCore import *
from PyQt4.QtGui import *

nameField = None
myDialog = None

def formOpen(dialog,layerid,featureid):
    global myDialog
    myDialog = dialog
    global nameField
    nameField = dialog.findChild(QLineEdit,"Name")
    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")

    # Disconnect the signal that QGIS has wired up for the dialog to the button box.
    buttonBox.accepted.disconnect(myDialog.accept)

    # Wire up our own signals.
    buttonBox.accepted.connect(validate)
    buttonBox.rejected.connect(myDialog.reject)

def validate():
  # Make sure that the name field isn't empty.
    if not nameField.text().length() > 0:
        msgBox = QMessageBox()
        msgBox.setText("Name field can not be null.")
        msgBox.exec_()
    else:
        # Return the form as accpeted to QGIS.
        myDialog.accept()