我正在使用qt设计器和后面的一些python代码为qgis 3制作自定义表单以进行验证。
但是,我有一个映射按钮操作确定和取消的问题。我首先断开按钮然后将它们连接到另一个函数以验证表单中的数据。
但是,当我尝试调用close函数(关闭窗口的形式)时,它只会使窗体消失,但窗口仍然存在
这是我的代码:
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()
谢谢您的帮助
我发现了另一种关闭整个窗口的方法。
我只是模拟了逃生键按下(通过使用pyautogui模块)并完成了这项工作。
您可以尝试此代码
myDialog.parent().close()
这对我有用
希望这可以帮助:
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()