[QGIS-Developer] Pyqgis Close init form

PIERRE Sylvain sylvain.pierre at alsace.eu
Tue Dec 20 01:04:55 PST 2022


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