How to redesign a window with Pyqt5?

Asked

Viewed 329 times

1

I have a CRUD made with Pyqt5 that adds a user to a database Postgresql and does a search to show registered users in a QT Listwidget.

However, when adding a new user, the screen does not "reload" and it is necessary to restart the program so that the added user appears in the list.

Follow prints demonstrating the problem. As soon as I add the user Test 2:

Assim que adiciona um usuário novo

But only when restarting the program, Test 2 appears in the list:

Imagem do programa reiniciado

I’ve tried using the commands:

widget.update()
widget.repaint()
widget.redraw()

But even so, without success.

Any idea how I could refresh the widget without having to restart the app?

Code that searches for and adds users to the list:

c.execute("SELECT * FROM dados")
busca = c.fetchall()
for row in busca:
    itm = QtWidgets.QListWidgetItem(row[1])
    self.listWidget.addItem(itm)

Code that adds a user to the database:

def clica_adicionar(self):

    nome = self.txt_nome_add.text()
    matricula = self.txt_matricula_add.text()
    cargo = self.txt_cargo_add.text()
    email = self.txt_email_add.text()

    c.execute("INSERT INTO dados (nome, matricula, cargo, email, cliente, relatorio, data) VALUES "
              "(%s, %s, %s, %s, %s, %s, %s)",
              (nome, matricula, cargo, email, cliente, rela, data))
  • Dear William, create a simple example of the fault and post the code, probably the problem is not how to redesign but how events are running parallel (this if they are parallel, because if it is not the case probably there is the fault).

  • I added the input codes in the database and search to display in the list. But, I believe not to be the problem since the program works. The problem is every time having to restart the program to update the list. Anyway, thanks for the warning.

  • 1

    Dear Guilherme, where is the CONNECT event to detect clicks? Instead of posting the actual code, it creates a very simplified version that causes the error and puts the code here, the smaller, the better :)

  • Got it! I’ll try, as soon as I can, to make a simpler example. But responding: The only connect in this case is on the add button of the first print. self.btn.clicked.connect(self.clica_add)

1 answer

1


I was able to update the widget and decided to respond here to help anyone with the same problem.

Whenever a new item is added, simply call the function refresh after the action.

On the model:

def clica_adicionar(self):
  nome = self.txt_nome_add.text()
  matricula = self.txt_matricula_add.text()
  cargo = self.txt_cargo_add.text()
  email = self.txt_email_add.text()

  c.execute("INSERT INTO dados (nome, matricula, cargo, email, cliente,   relatorio, data) VALUES "
          "(%s, %s, %s, %s, %s, %s, %s)",
          (nome, matricula, cargo, email, cliente, rela, data))

  widget.refresh()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.