不能 select 来自 QTableWidget 的整行数据

Can't select entire row of data from QTableWidget

问题陈述

我正在尝试从我的 Qtable 小部件中获取 select 行数据并将它们打印到我的控制台,这样我就可以测试一些东西,最终目标是能够绘制数据。但是我永远抓不到整行数据。

背景

我制作了一个 GUI,可以通过导入特定格式的 CSV 文件来嵌入多个 QTableWidget。目标是能够从相同或不同 table 的多行中提取数据,然后以并排方式绘制它们。每行数据将是它自己的数据集并有自己的图,但同一张图上会有多个图。

为了完成这个任务,我制作了一个名为 CompareWindow 的 window,它在按下名为 "Compare" 的 Qpushbutton 时打开。 window 提示用户输入 table 的名称以及他们希望绘制的 table 中的相应行。

提交此信息后,我有了可以参考的字典,它保存了所有已实例化的 QTable 对象。其中键是给予 table 的名称,这些名称连接到相应的 Table 对象。

问题

我尝试抓取行数据的两种主要方法是...

第一个想法是使用 TableObject.selectRow() 命令我会遍历我想要的行,但每当我这样做时它都会 return 一个非类型。

我尝试的第二种方法是迭代给定的行列,以便通过附加项目值逐一填充列表。然而,当我这样做时,它只会重复用相同的数字填充列表,这是我的 Qtable 中的第一个单元格。

即使我显式调用特定行或列,我也会得到相同的输出。提取的输出是 .12,这是我的 CSV 文件中第一个单元格的数字。

这是我遇到问题的相关代码。

    def initiateMultiPlot(self, tableV, rowV, PlotV):
    """
        1. Match TableName values with the key values in our TableDB
        2. When we find a  match look at that key's corresponding Table Object, and iterate
        through that objects rows and select the rows specified by rowV
        3.Call plot for those values

    """

    #calls my class and creates a blank figure where eventually we will plot data on
    f = CreateFigure.FigureAssembly()
    print("")
    for i in tableV:
        """
            tableV: is list of strings that represent assigned tablenames [Table1, Table2, Table3]
            rowV: is a list, containing lists representing rows from corresponding Tables the user wishes to plot.
                for example [[1,2],[3,4],[1]] means rows 1,2 from table1, rows 3,4 from table2... so on
            PlotV: is a string that is ethier "box" or "whisker" to tell what method to plot. Default right now 
            is to do a simple boxplot
        """
        print("Creating table instance")

        #Table Dictionary is setup so the names of the Tables (tableV) are the keys of the dictionary
        #and the actual table objects are referenced by these keys
        self.TableOBJ = self.TableDictionary[i]
        print("Data Type for the table object is..................{}".format(type(self.TableOBJ)))

        #empty list that will store our row data
        self.Elements = []
        try:
            for rows in rowV:

                for i in rows:
                    print("rowV value is... {}".format(rowV))
                    print("current row list{}".format(rows))
                    print("i value is {}".format(i))
                    print("itterating")

                    for j in range(self.TableOBJ.columnCount()):
                        print("i value is ...{}".format(i))
                        print("j value is .... {}".format(j))

                        #FIRST idea try selecting entire row of data
                        print("i value is ...{}".format(i))
                        print("j value is .... {}".format(j))

                        #entire row returns none-type
                        EntireRow = self.TableOBJ.selectRow(i)
                        print(EntireRow)

                        #selecteditems

                        #SECOND idea try using for loop and iterating through every value in a row
                        item = self.TableOBJ.itemAt(i,j)

                        #explicit call for (row 1, col 1) and  (row 3, col 3), both which output .12
                        print(self.TableOBJ.itemAt(1,1).text())
                        print(self.TableOBJ.itemAt(3,3).text())
                        print("printing item...... {}".format(item))
                        element = item.text()
                        print(element)

                        #list of .12
                        self.Elements.append(element)

                    #elements = [self.TableOBJ.item(i, j).text() for j in range(self.TableOBJ.columnCount()) if
                    #            self.TableOBJ.item(i, j).text() != ""]
                    #print(elements)

        except Exception as e:
            print(e)

        print(self.Elements)

这是我的 GitHub link,其中包含我的所有文件:https://github.com/Silvuurleaf/Data-Visualize-Project

问题出现在我的文件Perspective.py中的initiateMultiPlot方法中。文件 CompareWindow.py 向我的 Perspective.py 发送信号并连接到 initateMultiPlot。请询问是否需要更深入的解释。

根据 documentation:

QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const

Returns the item at the position equivalent to QPoint(ax, ay) in the table widget's coordinate system, or returns 0 if the specified point is not covered by an item in the table widget.

也就是说,returns 给定的项目 x 和 y 是相对于 QTableWidget 的图形坐标,显然不是您要找的东西。

您必须使用 item():

QTableWidgetItem *QTableWidget::item(int row, int column) const

Returns the item for the given row and column if one has been set; otherwise returns 0.

但在您的情况下将不起作用,除非您进行以下更改:

class CreateTable(QTableWidget):
     ....
            for j in range(0, m):
                self.item = QTableWidgetItem(str(round(ValList[j], 6)))
                # print("{}, {}".format(i, j))
                self.setItem(i, j, self.item)

至:

class CreateTable(QTableWidget):
     ....
            for j in range(0, m):
                item = QTableWidgetItem(str(round(ValList[j], 6)))
                # print("{}, {}".format(i, j))
                self.setItem(i, j, item)

也就是说,您将 self.item 更改为 item

问题是乍一看错误是相当困难的,QTableWidget class 有一个 item() 函数,但是当你使用 self.item 语句时你正在替换那个调用,即当 python 读取该语句时它将使用属性而不是函数,所以你得到错误:

TypeError 'xxx' object is not callable