在Pyqt4中使用不同的QMainWindow刷新QMainWindow

时间:2019-02-26 20:48:19

标签: python-3.x pyqt4 qmainwindow qlistwidget qlistwidgetitem

我需要您的帮助来解决此问题,当我从另一个不在主体中的QMainWindow添加新寄存器时,我想刷新pyqt4中的QlistWidget,我尝试使用“ repaint()”但它不起作用,我会更好地理解代码

这是我添加数据库中所有项目的主窗口

class Psicologo(QMainWindow):
    def __init__(self, parent=None):
        super(Psicologo, self).__init__(parent)
        uic.loadUi('inicio.ui', self)
        self.button_volver.clicked.connect(self.regresar)
        self.button_nvo_psicologo.clicked.connect(self.nuevoPsicologo)
        query = "SELECT * FROM user"
        cursor, conexion = CreateConexion()
        cursor.execute(query)
        registro = cursor.fetchall()
        for i in registro:
            self.lista_psicologos.addItem(i[1]+" "+i[2]+" "+i[3])


    def regresar(self):
        self.parent().show()
        self.close()

    def nuevoPsicologo(self):
        self.hide()
        nuevo_form = AltaPsico(self)
        nuevo_form.show()

这是我创建新寄存器的另一个窗口

class AltaPsico(QMainWindow):
    def __init__(self, parent=None):
        super(AltaPsico, self).__init__(parent)
        uic.loadUi('alta_paciente.ui', self)
        self.combo_sexo.hide()
        self.label_12.hide()
        self.text_comentario.hide()
        self.label_14.hide()
        self.line_correo_2.hide()
        self.label_13.hide()
        self.button_volver.clicked.connect(self.regresar)
        self.button_guardar.clicked.connect(self.guardar)
        self.button_eliminar.hide()

    def guardar(self):
        nombre = self.line_nombre.text()
        app = self.line_app.text()
        apm = self.line_apm.text()
        domicilio = self.line_domicilio.text()
        edad = self.line_edad.text()
        telefono = self.line_telefono.text()
        especialidad = self.line_especialidad.text()
        correo = self.line_correo.text()
        usuario = self.line_usuario.text()
        password = self.line_password.text()
        pass2 = self.line_password_2.text()
        if password == pass2:
            if edad.isdigit():
                if validate_email(correo):
                    if telefono.isdigit():
                        query = "INSERT INTO user(idUser, Nombre, ApPaterno, ApMaterno, Domicilio, Edad, Telefono, Especialidad, Correo, Usuario, Password, Tipo) VALUES (0,'"+nombre+"','"+app+"', '"+apm+"', '"+domicilio+"', '"+edad+"', '"+telefono+"', '"+especialidad+"', '"+correo+"', '"+usuario+"', '"+password+"', 2);"
                        print (query)
                        cursor, conexion = CreateConexion()
                        cursor.execute(query)
                        msgBoxCancel = QtGui.QMessageBox( self )
                        msgBoxCancel.setIcon( QtGui.QMessageBox.Information )
                        msgBoxCancel.setText( "Usuario registrado con exito" )
                        msgBoxCancel.addButton( QtGui.QMessageBox.Ok )
                        msgBoxCancel.exec_()
                        conexion.commit()
                        conexion.close()
                        self.parent().show()
                        self.QMainWindow.update()
                        self.close()
                    else:
                        msgBoxCancel = QtGui.QMessageBox( self )
                        msgBoxCancel.setIcon( QtGui.QMessageBox.Information )
                        msgBoxCancel.setText( "¡Telefono invalido, ingrese telefono valido!" )
                        msgBoxCancel.addButton( QtGui.QMessageBox.Ok )
                        msgBoxCancel.exec_()
                else:
                    msgBoxCancel = QtGui.QMessageBox( self )
                    msgBoxCancel.setIcon( QtGui.QMessageBox.Information )
                    msgBoxCancel.setText( "¡Correo invalido, ingrese correo valido!" )
                    msgBoxCancel.addButton( QtGui.QMessageBox.Ok )
                    msgBoxCancel.exec_()
            else:
                msgBoxCancel = QtGui.QMessageBox( self )
                msgBoxCancel.setIcon( QtGui.QMessageBox.Information )
                msgBoxCancel.setText( "¡Ingrese una edad valida!" )
                msgBoxCancel.addButton( QtGui.QMessageBox.Ok )
                msgBoxCancel.exec_()
        else:
            msgBoxCancel = QtGui.QMessageBox( self )
            msgBoxCancel.setIcon( QtGui.QMessageBox.Information )
            msgBoxCancel.setText( "¡La contraseña no coincide, intentelo de nuevo!" )
            msgBoxCancel.addButton( QtGui.QMessageBox.Ok )
            msgBoxCancel.exec_()


def regresar(self):
    self.parent().repaint()
    self.parent().show()
    self.close()

当寄存器成功结束时,此窗口会自动关闭,但是当主体再次显示时,listWidget会在开头显示相同的寄存器。

我希望这个社区能够为我提供帮助,我一直在搜索有关此主题的信息,但我什么都找不到。

1 个答案:

答案 0 :(得分:0)

如果修改了数据库,则不会通知GUI,因此必须再次请求数据:

class Psicologo(QMainWindow):
    def __init__(self, parent=None):
        super(Psicologo, self).__init__(parent)
        uic.loadUi('inicio.ui', self)
        self.button_volver.clicked.connect(self.regresar)
        self.button_nvo_psicologo.clicked.connect(self.nuevoPsicologo)
        self.load_from_db()

    def load_from_db(self):
        self.lista_psicologos.clear()
        query = "SELECT * FROM user"
        cursor, conexion = CreateConexion()
        cursor.execute(query)
        registro = cursor.fetchall()
        for i in registro:
            self.lista_psicologos.addItem("{} {} {}".format(i[1], i[2], i[3]))

    def regresar(self):
        self.parent().show()
        self.close()

    def nuevoPsicologo(self):
        self.hide()
        nuevo_form = AltaPsico(self)
        nuevo_form.show()

def guardar(self):
    # ...
    conexion.commit()
    conexion.close()
    self.parent().show()
    self.parent().load_from_db()