1

我需要你的帮助来解决这个问题,当我从另一个不在主体中的 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 会在开始时显示相同的注册。

我希望这个社区可以帮助我,我一直在搜索这个主题,但我找不到任何东西。

4

1 回答 1

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()
于 2019-02-26T22:57:36.093 回答