Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving cell data from a selected cell in a tablewidget

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

like image 449
Sam McKay Avatar asked Jan 29 '13 17:01

Sam McKay


2 Answers

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!

like image 141
danodonovan Avatar answered Nov 14 '22 08:11

danodonovan


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()
like image 1
Amgad Elsaiegh Avatar answered Nov 14 '22 08:11

Amgad Elsaiegh