Python PyQt: QTableWidget - get cell value based on header string and selected row

人走茶凉 提交于 2019-11-27 15:22:33
panofish

If you do that you got: "local variable 'matchcol' referenced before assignment"

To fix that you should return the cell inside if loop:

#===================================================================
# given a tablewidget which has a selected row...
# return the column value in the same row which corresponds to a given column name
# fyi: columnname is case sensitive
#===================================================================

def getsamerowcell(widget,columnname):

    row = widget.currentItem().row()
    #col = widget.currentItem().column()

    #loop through headers and find column number for given column name
    headercount = widget.columnCount()
    for x in range(0,headercount,1):
        headertext = widget.horizontalHeaderItem(x).text()
        if columnname == headertext:
            cell = widget.item(row, x).text()   # get cell at row, col
            return cell

Once you have the row and column of the cell, you can use QTableWidget.item to get the QTableWidgetItem, from which you can get the text and/or the stored data.

So in your slot, if you have row and column, you can see which letter this column corresponds to, and from there determine which column you want to get the data from.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!