Python 从表格小部件中的选定单元格中检索单元格数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14588479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving cell data from a selected cell in a tablewidget
提问by Sam McKay
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
我正在制作一个股票控制程序,但在获取选定单元格的值时遇到了问题,我知道我需要使用“QtGui.QTableWidget.currentRow”和“QtGui.QTableWidget.currentColumn”来获取项目的位置。但是我似乎无法让它工作,因为当调用函数时没有选择任何东西,所以它返回 -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?
有谁知道如何获取它以便每次用户选择一个单元格时它都会运行“QtGui.QTableWidget.currentRow”和“QtGui.QTableWidget.currentColumn”?
i think the code i need to get the actual data once i have the co-ords is QtGui.QTableWidget.item ?
我认为一旦我有了坐标,我需要获取实际数据的代码是 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
我正在使用 python 3.2 和 pyqt 4
any help would be appreciated
任何帮助,将不胜感激
Thank you
谢谢
Sam
山姆
采纳答案by danodonovan
When the QTableWidgetsees that someone has clicked one of it's cells, it will emit a cellClickedevent - which you need to connect to. Maybe something like
当QTableWidget看到有人点击了它的一个单元格时,它会发出一个cellClicked事件 - 您需要连接到该事件。也许像
self.table.cellClicked.connect(self.cell_was_clicked)
could be in your setup code,
and the function cell_was_clickedmight be something like
可能在您的设置代码中,该功能cell_was_clicked可能类似于
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 currentRowand currentColumnas 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 itemAtinstead of just item- as you will get the item at the cell, not it's contents. Use the .text()function of QTableWidgetItemto get at the contents.
我没用过currentRow,currentColumn因为你想在click. 该函数记录在此处(抱歉,我更喜欢 pyside - 它与 PyQT 几乎相同)。还要注意itemAt而不是仅仅item- 因为您将在单元格中获得项目,而不是它的内容。使用 的.text()函数QTableWidgetItem获取内容。
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' :-)
注意 - 这是使用“插槽和信号”,特别是“新风格”。当您使用 PyQT4 和 Python 3 时,“新东西”应该没有问题:-)
You might consider browsing a slots and signals tutorial- that might straighten a few of these abstract concepts out. Good luck!
您可能会考虑浏览插槽和信号教程- 这可能会理顺这些抽象概念中的一些。祝你好运!

