7

I am making a stock control program and i have hit a problem with getting the value of a selected cell, i know i need to use "QtGui.QTableWidget.currentRow" and "QtGui.QTableWidget.currentColumn" to get the item's position. However i cannot seem to get this to work because when the functions are called nothing has been selected and so it returns -1,-1

Does anyone know how to get it so it runs the "QtGui.QTableWidget.currentRow" and "QtGui.QTableWidget.currentColumn" everytime the user selects a cell?

i think the code i need to get the actual data once i have the co-ords is QtGui.QTableWidget.item ?

This is the code i am using to get the row and column:

row = self.table.currentRow
column = self.table.currentColumn
self.ID = self.table.item(row, column)

when the user clicks a button to add stock the program should then use the product code it will have just got to make the change to the database after getting the quantity added from the user

I am using python 3.2 and pyqt 4

any help would be appreciated

Thank you

Sam

Sam McKay
  • 73
  • 1
  • 1
  • 9
  • if you could post a code snippet it might i) help us understand and ii) help you explain – danodonovan Jan 29 '13 at 17:19
  • UPDATE: I am now able to retrieve the cell item, however it is returning the actual item rather than the data def getCell(self): row = self.table.currentRow() print(row) self.ID = self.table.item(row, 0) print(self.ID) this returns rather than "14" can anyone help? – Sam McKay Jan 29 '13 at 18:02
  • Have a look at my updated answer :-) you want to call `item.text()` on the item you've had returned. – danodonovan Jan 29 '13 at 18:05

2 Answers2

24

When the QTableWidget sees that someone has clicked one of it's cells, it will emit a cellClicked event - which you need to connect to. Maybe something like

self.table.cellClicked.connect(self.cell_was_clicked)

could be in your setup code, and the function cell_was_clicked might be something like

def cell_was_clicked(self, row, column):
    print("Row %d and Column %d was clicked" % (row, column))
    item = self.table.itemAt(row, column)
    self.ID = item.text()

I've not used currentRow and currentColumn as you want a response on the click. This function is documented here (sorry, I prefer pyside - it's pretty much the same as PyQT). Note also itemAt instead of just item - as you will get the item at the cell, not it's contents. Use the .text() function of QTableWidgetItem to get at the contents.

Note - this is using 'slots and signals' and in particular the 'new style'. As you're using PyQT4 and Python 3 you should have no trouble with 'new stuff' :-)

You might consider browsing a slots and signals tutorial - that might straighten a few of these abstract concepts out. Good luck!

danodonovan
  • 19,636
  • 10
  • 70
  • 78
  • row = self.table.currentRow() print(row) self.item = self.table.item.text(row, 0) self.ID = self.item.text() print(self.ID) this doesn't want to work for me? i get this error Traceback (most recent call last): File "F:\College\Computing\Stock control\Coding\Stock Control GUI.py", line 453, in getCell self.item = self.table.item.text(row, 0) AttributeError: 'builtin_function_or_method' object has no attribute 'text' – Sam McKay Jan 29 '13 at 18:16
  • So that sounds like `item` isn't a `QTableWidgetItem` - see what `print(type(self.item))` produces and then look that up in the manual. – danodonovan Jan 29 '13 at 18:19
  • ahhh, i just noticed i slight error in my code, i'd kept item instead of using itemAt. its working perfectly now, i can carry on and get this program finished :L – Sam McKay Jan 29 '13 at 18:21
  • just wondering if i could get a little more help, how do i convert this [('example data', 'example data', 'example data', 'example data')] to this [(example data),(example data),(example data),(example data)] ?? – Sam McKay Jan 29 '13 at 19:10
  • You probably aught to open a new question... If `example data` is a numerical value, then if `x = [('example data', 'example data', 'example data', 'example data')]` then `[float(e) for e in x[0]]` will return a list of floats - and you have a nested list, hence the `x[0]` – danodonovan Jan 29 '13 at 19:17
  • okay, no worries then, i'll keep trying and maybe open a new question if don't get anywhere – Sam McKay Jan 29 '13 at 19:22
1

here is the code that worked for me:

def get_selected_cell_value():
    current_row = self.sold_items_details_table.currentRow()
    current_column = self.sold_items_details_table.currentColumn()
    cell_value = table.item(current_row, current_column).text()