Validate field and do not close the form

2

I am working on a plugin for Qgis, in the form the user enters the data and one of them is the name of the file, so I validate the field and verify that the same name does not exist, the problem is that if there is a sample a message and the form is closed, not allowing the user to modify the name.

The function that verifies the name of the document is the following:

def verificaArchivo(self,file):

   documentoNuevo = os.path.isfile(file)

   if documentoNuevo is True:
       QMessageBox.information(None, "ERROR", "El nombre {} ya EXISTE!".format(self.dlg.nombreArchivo.text()))
   else:
    return False

And here is where it runs

def run(self):
    """
   QMessageBox.information(None, "valores", ",".join([str(x[0]) for x in cursorLista]))
   QMessageBox.information(None, "valores", "idLote={}".format(self.dlg.lote.text()))

    """
    self.dlg.lote.clear()
    self.dlg.referencia.clear()
    self.dlg.nombreArchivo.clear()
    self.dlg.orden.clear()
    self.dlg.comboBox.clear()
    self.dlg.desLote.clear()

    self.dlg.lote.textChanged.connect(self.onTextChanged)
    self.dlg.lote.textChanged.connect(self.buscaDescripcion)

    ix = self.dlg.comboBox.currentIndex()
    idSeccion = self.dlg.comboBox.itemData(ix)

    self.dlg.show()

    # Run the dialog event loop

    result = self.dlg.exec_()

    # See if OK was pressed
    if result:

        #Guarda imagen en servidor

        rutaArchivo = "D:/temp/"+"Lote-"+self.dlg.lote.text()+"_"+self.dlg.nombreArchivo.text()+".png"

        resulArchivo = self.verificaArchivo(rutaArchivo)

        if resulArchivo is False:

            self.iface.mapCanvas().saveAsImage(rutaArchivo)
            self.insertaInformes(self.dlg.lote.text(),self.dlg.referencia.toPlainText(),self.dlg.nombreArchivo.text()+".png",idSeccion,self.dlg.orden.text())

        pass

How can I achieve that if the function returns True, allow the user to modify the name of the document in self.dlg.filename.text ()

class SaveInformesDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(SaveInformesDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)

Code

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>SaveInformesDialogBase</class>
 <widget class="QDialog" name="SaveInformesDialogBase">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>516</width>
    <height>355</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>GuardaInformes</string>
  </property>
  <widget class="QDialogButtonBox" name="button_box">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>310</y>
     <width>171</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>90</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Referencia</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="orden">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>230</y>
     <width>81</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Numero Lote</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>230</y>
     <width>81</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Orden</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_4">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>50</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Seccion</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="nombreArchivo">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>270</y>
     <width>351</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="lote">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>10</y>
     <width>81</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QComboBox" name="comboBox">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>50</y>
     <width>351</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label_6">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>270</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
    </font>
   </property>
   <property name="text">
    <string>Nombre Archivo</string>
   </property>
  </widget>
  <widget class="QTextEdit" name="referencia">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>90</y>
     <width>351</width>
     <height>121</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="desLote">
   <property name="geometry">
    <rect>
     <x>240</x>
     <y>10</y>
     <width>261</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>button_box</sender>
   <signal>accepted()</signal>
   <receiver>SaveInformesDialogBase</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>20</x>
     <y>20</y>
    </hint>
    <hint type="destinationlabel">
     <x>20</x>
     <y>20</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>button_box</sender>
   <signal>rejected()</signal>
   <receiver>SaveInformesDialogBase</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>20</x>
     <y>20</y>
    </hint>
    <hint type="destinationlabel">
     <x>20</x>
     <y>20</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>
    
asked by Sebastian 07.08.2018 в 14:10
source

1 answer

1

One way to prevent the window from closing is to disable the Ok button that closes the dialog, for this we use setDisabled (), we use the textChanged () signal from the QLineEdit to do that task:

def __init__(self, iface):
    ...
    self.dlg = SaveInformesDialog()
    self.dlg.lote.textChanged.connect(self.onTextChanged)
    self.dlg.lote.textChanged.connect(self.buscaDescripcion)
    self.dlg.lote.textChanged.connect(self.verificar)
    self.dlg.nombreArchivo.textChanged.connect(self.verificar)

...

def run(self):
    self.dlg.lote.clear()
    self.dlg.referencia.clear()
    self.dlg.nombreArchivo.clear()
    self.dlg.orden.clear()
    self.dlg.comboBox.clear()
    self.dlg.desLote.clear()

    ix = self.dlg.comboBox.currentIndex()
    idSeccion = self.dlg.comboBox.itemData(ix)

    # Run the dialog event loop

    result = self.dlg.exec_()

    # See if OK was pressed
    if result:
        self.iface.mapCanvas().saveAsImage(rutaArchivo)
        self.insertaInformes(self.dlg.lote.text(),self.dlg.referencia.toPlainText(),self.dlg.nombreArchivo.text()+".png",idSeccion,self.dlg.orden.text())

def verificar(self):
    rutaArchivo = "D:/temp/Lote-{}_{}.png".format(self.dlg.lote.text(), self.dlg.nombreArchivo.text())
    existe_archivo = os.path.isfile(rutaArchivo)

    btn = self.dlg.button_box.button(QtWidgets.QDialogButtonBox.Ok)
    btn.setDisabled(existe_archivo)
    
answered by 07.08.2018 / 15:31
source