How to call a qdialog several times but always start from the default values?

0

I have a QMainWindow from which I call a Qdialog, the problem is that I use the Qdialog to obtain data, everything works fine when it is used for the first time, but when I use it again, if I do not introduce new values and I simply close it , returns the last returned values. When I call it from MainWindow I do it like this:

self.dialogo=secundal.buscar_cups(resultado,resp)
self.dialogo.exec_()
cod,desc=secundal.buscar_cups(resultado,resp).retorno()

What I have in the secondary file is the following:

import sys
from busqueda_cups import Ui_Dialog
from PyQt4 import QtCore, QtGui
import os
import MySQLdb

id_consulta=''
descripcion=''


class buscar_cups(QtGui.QDialog):
def __init__(self,resultado,num_filas):
    QtGui.QWidget.__init__(self)
    self.ventana = Ui_Dialog()
    self.ventana.setupUi(self)

    self.ventana.tableWidget.setRowCount(num_filas)
    for i in range(0,len(resultado)):
        for j in range(0,2):
            self.ventana.tableWidget.setItem(i,j,QtGui.QTableWidgetItem(str(resultado[i][j])))
    self.connect(self.ventana.tableWidget,QtCore.SIGNAL('cellDoubleClicked (int,int)'),self.seleccion)

def seleccion(self):
    global id_consulta,descripcion
    id_consulta=''
    descripcion=''
    cod,desc=self.ventana.tableWidget.selectedItems()
    id_consulta=cod.text()
    descripcion=desc.text()
    self.close()


def retorno(self):
    return id_consulta,descripcion
    
asked by LJJT 01.06.2018 в 17:35
source

1 answer

0

The return of the method retorno (worth the redundancy) depends on the value of the variables id_consulta and descripcion .

These variables are global variables to the module secundal , when the module is imported it is also executed, so both variables are created by pointing to empty strings. From this moment all the possible instances of class buscar_cups will share these variables and their modification affects all instances or future instances of the class or any object that uses them in that module.

That is, when you import the module, global variables are initialized to an empty string, when instances% co_of% for the first time if it modifies its values using the buscar_cups method, these changes are maintained for the next instance of the class.

This is one of the reasons why there is that mantra that global variables are "perverse", the solution in your case is simply to make both variables are instance attributes, created and initialized in seleccion and therefore own for each instance of the class.

import sys
from busqueda_cups import Ui_Dialog
from PyQt4 import QtCore, QtGui
import os
import MySQLdb



class BuscarCups(QtGui.QDialog):
    def __init__(self, resultado, num_filas, *args, **kwargs):
        super(BuscarCups, self).__init__(*args, **kwargs)
        self.id_consulta = ''
        self.descripcion = ''
        self.ventana = Ui_Dialog()
        self.ventana.setupUi(self)

        self.ventana.tableWidget.setRowCount(num_filas)
        for i in range(0,len(resultado)):
            for j in range(0,2):
                self.ventana.tableWidget.setItem(i,j,QtGui.QTableWidgetItem(str(resultado[i][j])))
        self.connect(self.ventana.tableWidget, QtCore.SIGNAL('cellDoubleClicked (int,int)'), self.seleccion)

    def seleccion(self):
        cod, desc = self.ventana.tableWidget.selectedItems()
        self.id_consulta = cod.text()
        self.descripcion = desc.text()
        self.close()

    def retorno(self):
        return self.id_consulta, self.descripcion

On the other hand, in your main window you should not create a new instance to call __init__ :

dialogo = secundal.BuscarCups(resultado, resp)
dialogo.exec_()  # Dialogo modal
cod, desc = dialogo.retorno()
    
answered by 01.06.2018 в 20:02