[QGIS-Developer] Pyqgis Close init form

Richard Duivenvoorde rdmailings at duif.net
Tue Dec 20 02:33:30 PST 2022


Ok, did some testing...

Mmm something fishy is going on..
First time I used: dialog.close()  I got an python exception telling me that QgsAttributeForm didn't have a close() ..
So the 'dailog' is actually not a QDialog but the QgsAttributeForm (which is a QWidget) ON a QDialog..
But... I could never replicate that anymore after that first time ???

Then I was able to replicate your crash by calling
dialog.parent().reject()
But tried
dialog.parent().close()
dialog.parent().hide()
but those were OK (but not working the way you want)

BUT: I think the reason close/hide are not working is because this is called BEFORE showing the dialog.
So the close/hide are actually called before your dialog is even shown.
And THEN QGIS will show the dialog (and you are too late)?

So I think what you want is not possible?
You could maybe create a new ui/dialog in which tells the user something (besides showing the other ui)?
But closing the dialog (before it is even shown) doesn't seem possible.

I'm not 100% sure of all this, so if others want to try or know better, please let me know

Regards,

Richard Duivenvoorde

PS tip to debug: when you use print-statements in your python init script, AND open the python console, the output will show up there!

On 12/20/22 10:04, PIERRE Sylvain wrote:
> When adding a new feature (polygon) I would like to close form when centroid does not intersect a polygon from an other layer
> Here is my full python init form dialog function:
> 
> # -*- coding: utf8 -*-
> from qgis.core import QgsVectorLayer, QgsFeatureRequest, QgsRectangle, QgsGeometry, QgsProject, QgsPointXY
> import qgis.utils
> from PyQt5.QtWidgets  import QDialogButtonBox, QLineEdit, QComboBox
> from PyQt5.QtCore import QObject
> import os.path
> import getpass
> 
> def dealWithPoly(dialog, layerid, featureid):
> 
>      global editMode
>      global iface
> 
>      iface = qgis.utils.iface
> 
>      geom = featureid.geometry()
>      if geom:
>          editMode = 'insert'
>          
>          layers = QgsProject.instance().mapLayers().values()
>          canvas = iface.mapCanvas()
> 
>          geo_point = geom.centroid()
> 
>          canvas_crs_txt = canvas.mapSettings().destinationCrs().authid()
>          canvas_crs = canvas_crs_txt.split(':')[1]
> 
>          layerNameRPG = 'MAEC2023 parcelles'
>          testlayerRPG = QgsProject().instance().mapLayersByName(layerNameRPG)
>          
>          if testlayerRPG:
>              bv = testlayerRPG[0]
> 
>              layer_crs_txt = bv.crs().authid()
>              layer_crs = layer_crs_txt.split(':')[1]
> 
>              if canvas_crs != layer_crs:
>                  xform = QgsCoordinateTransform(QgsCoordinateReferenceSystem('EPSG:'+canvas_crs), QgsCoordinateReferenceSystem('EPSG:'+layer_crs), QgsProject.instance())
>                  geo_point.transform(xform)
>                  
>              find_parcelle = False
>              
>              polygon = [feature for feature in bv.getFeatures()]
>              for pol in polygon:
>                  geo_pol = pol.geometry()
>                  if geo_pol.contains(geo_point):
>                      pacageVal = pol['Pacage']
>                      numIlot = str(pol['Num_ilot'])
>                      find_parcelle = True
>                      break
>                      
>              if not find_parcelle:
>                  txt = u"Saisie en dehors des limites du RPG"
>                  iface.messageBar().pushMessage(txt, level=Qgis.Warning)
>                  #dialog.parent().reject()
>                  dialog.close()
> 
>                  #print(dialog.parent())
>              else:
>                  linedit_pacage = dialog.findChild(QLineEdit,"PACAGE")
>                  linedit_pacage.setText(pacageVal)
>                          
>                  linedit_numilot = dialog.findChild(QLineEdit,"NUM_ILOT")
>                  linedit_numilot.setText(numIlot)
> 
> 
>                      
> 
>                  
> 
> 
> 
>    
> 
> -----Message d'origine-----
> De : QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> De la part de Richard Duivenvoorde via QGIS-Developer
> Envoyé : mardi 20 décembre 2022 09:44
> À : qgis-developer at lists.osgeo.org
> Objet : Re: [QGIS-Developer] Pyqgis Close init form
> 
> Soyez vigilant : ce courriel provient d'une organisation externe à la CeA.
> • Assurez-vous d’abord qu’il ne s’agit pas d’un email malveillant avant de cliquer sur tout lien ou pièce jointe.
> Apprendre à reconnaître un email de phishing.<https://intranet.alsace.eu/actualites/apprenez-a-reconnaitre-un-email-phishing/>
> 
> 
> Can you provide a little more code?
> 
> And more info where exactly it is called (in the startup.py or in some form init code?
> 
> Is 'dialog' available in the code part in which you try to close it?
> 
> Regards,
> 
> Richard Duivenvoorde
> 
> On 12/20/22 09:24, PIERRE Sylvain via QGIS-Developer wrote:
>> Hi all,
>>
>> I can not achieve closing a form in init pyqgis fonction. I try :
>>
>>                   dialog.close()
>>
>> But form stay without any widgets
>>
>> I try
>>
>>                   dialog.parent().close()
>>
>> Does nothing
>>
>>                   dialog.parent().reject()
>>
>> Qgis crash
>>
>> Any hints ?
>>
>> Thanks
>>
>> **
>>
>>
>>
>> Sylvain PIERRE**
>>
>> Chef de projet système d’information
>>
>> Direction des Systèmes d’Information et du Développement Numérique
>>
>> Service Projets et Ingénierie Numérique
>>
>> *Collectivité européenne d’Alsace*
>>
>> Tél : 03 88 76 68 88
>>
>> sylvain.pierre at alsace.eu <mailto:sylvain.pierre at alsace.eu>
>>
>> www.alsace.eu <http://www.alsace.eu>
>>
>> facebook <http://www.facebook.com/toutelalsace>twitter <http://www.twitter.com/toutelalsace>insta <http://www.instagram.com/toutelalsace>
>>
>>
>> _______________________________________________
>> QGIS-Developer mailing list
>> QGIS-Developer at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the QGIS-Developer mailing list