2

I have a qtablewidget :

table = QtWidgets.QTableWidget(0, 5)

I update it :

nbrWidget = QtWidgets.QTableWidgetItem(str(item.item_nbr))
table.setItem(0,0,nbrWidget)

Now the user edit it to correct it. How can I catch the value when he press returen, and launch an existing updateData()

I have read some doc but it is not clear between delegates and userrole. I'd like to keep it as simple as possible.

Sylvain Page
  • 581
  • 1
  • 7
  • 21
  • Are you familiar with `signals` and how you connect Widgets to actions? – offeltoffel Sep 18 '17 at 12:39
  • Yes already used in my little project (subclassed mouseenvent in dedicated qgraphicsscene) – Sylvain Page Sep 18 '17 at 12:54
  • There is a signal called `ReturnPressed` which you can connect to your QTableWidget. You can use it to pass the item number to your `updateData()` where the new input is being read. – offeltoffel Sep 18 '17 at 12:56
  • I tried this self.table.returnPressed.connect(self.modifyItem) => 'QTableWidget' object has no attribute 'returnPressed', did I miss something ? It seems returnPressed exist for QlineEdit or other widget, not for QTableWidget, or I have to explicitly create/modify the signal ? – Sylvain Page Sep 18 '17 at 13:09
  • 1
    It seems that QTableWidgets do not accept the returnPressed connection (unlike QLineEdits). But in any case you can override the `keyPressEvent`. It's not the simple solution that you wished for, but it should work: https://stackoverflow.com/questions/23961355/python-qt-how-to-catch-return-in-qtablewidget – offeltoffel Sep 18 '17 at 13:14
  • 1
    Yes does the job, just having a problem because with self.currentItem().text() I get the previous value not the new one. Need to invest it. Anyway thanks I'll post the solution when I'll solve it – Sylvain Page Sep 18 '17 at 14:24
  • 1
    Maybe call `QApplication.processEvents()` first and then ask for the currentItem().text() – offeltoffel Sep 18 '17 at 14:33
  • Yes perfect ! thanks I guess I would have never found it myself. – Sylvain Page Sep 18 '17 at 21:56

0 Answers0