我需要你的帮助来解决这个问题,我想在pyqt4中刷新一个QlistWidget,当我从另一个QMainWindow添加一个新的寄存器而不是在本人时,我尝试了“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会在开始时显示相同的寄存器。
我希望这个社区可以帮助我,我一直在搜索这个主题,但我找不到任何东西。
如果修改了数据库,则不会通知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()