Kiper
Kiper

Reputation: 339

Updating mysql database with pyqt and qtablewidget

I have a program that stores my database data in a QtableWidget. I would like to add EventListener so whenever user edits any column in a row, It will update immediately the data in the server.

I am trying to think of a way doing this. The idea is to send an UPDATE query to servers database. But i stuck on finding a way of making it to see the change and update immediately.

Or updating when button clicked after many rows been edited. But then it have to store all the changes so I think the first option is better.

Any advise will be great!

Thanks ahead!

Upvotes: 1

Views: 2038

Answers (1)

Peter Wang
Peter Wang

Reputation: 1838

I agree with you; I think the first option is the better one. Here is a way you could achieve that: you could wait for the user to make the change (the table by default should be editable) and when the user presses enter, process the event. To do that, take a look at BeetDemGuise's answer.

After the enter key has been pressed, a signal will be emitted, and you can connect it to a slot function that will look at the current cell data and update it in the database. e.g. signal.connect(handle_signal). In your handle_signal(), you can get the current text (mytable.currentItem().text()), then make the change in the database. If you're using SqlAlchemy it will look something like:

table = self.values.update()
self.engine.execute(table.values(value="[the current text]".
                            where(table.id == id))

Of course, this will vary depending on what ORM you're using.

Upvotes: 2

Related Questions