Python 根据标题字符串和选定行获取单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16367090/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:23:35  来源:igfitidea点击:

get cell value based on header string and selected row

pythonpyqtqtablewidget

提问by panofish

For example, I have a PyQt QTableWidget which has 3 columns and 2 rows. The column headers are labeled A, B, and C.

例如,我有一个 PyQt QTableWidget,它有 3 列和 2 行。列标题标记为 A、B 和 C。

A B C
1 2 3
4 5 6

This is the excerpt from my current source:

这是我当前来源的摘录:

class myform(QtGui.QMainWindow):

    def __init__(self, parent=None):

        super(myform, self).__init__(parent)

        self.ui = Ui_mygui()
        self.ui.setupUi(self)

        self.ui.mytablewidget.cellClicked.connect(self.cell_was_clicked)

    @QtCore.pyqtSlot() # prevents executing following function twice
    def cell_was_clicked(self):
        row = self.ui.mytablewidget.currentItem().row()
        print "row=",row
        col = self.ui.mytablewidget.currentItem().column()
        print "col=",col
        item = self.ui.mytablewidget.horizontalHeaderItem(col).text()
        print "item=",item

My code works and when I select a row in my table.. I get the correct row and col numbers from the selection.

我的代码有效,当我在表中选择一行时.. 我从选择中得到正确的行和列号。

What is the code needed to return a cell value for the selected row given a specified header name? If I select row 2 cell 1 ... how can I get the cell value of column C on the same row?

返回给定标题名称的选定行的单元格值所需的代码是什么?如果我选择第 2 行单元格 1 ......我怎样才能在同一行上获得 C 列的单元格值?

采纳答案by panofish

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

如果你这样做,你会得到:“分配前引用的局部变量‘matchcol’”

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

要解决这个问题,您应该在 if 循环中返回单元格:

#===================================================================
# 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

回答by jadkik94

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

获得单元格的行和列后,您可以使用QTableWidget.item获取 QTableWidgetItem,从中可以获取文本和/或存储的数据

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.

所以在你的槽中,如果你有行和列,你可以看到这个列对应哪个字母,并从那里确定你想要从哪一列获取数据。