无法从QTableWidget中选择整行数据

2024-06-16 11:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

问题陈述

我试图从我的QtableWidget中选择数据行并将它们打印到我的控制台,这样我就可以测试一些东西,最终目标是能够绘制数据。然而,我永远无法获取整行数据

背景

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

为了完成这项任务,我创建了一个名为CompareWindow的窗口,当按下名为“Compare”的Qpushbutton时,该窗口将打开。该窗口提示用户输入表格的名称以及他们希望打印的表格中的相应行

提交此信息后,我有了可以引用的字典,它保存了所有已实例化的QTableObjects。其中键是指定给连接到相应表对象的表的名称

问题

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

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

我尝试的第二种方法是迭代给定的行和列,以便通过追加项值逐个填充列表。然而,当我这样做时,它只是重复地用相同的数字填充列表,这是我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链接,包含我的所有文件:https://github.com/Silvuurleaf/Data-Visualize-Project

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


Tags: andthe数据inselfformatforis
1条回答
网友
1楼 · 发布于 2024-06-16 11:07:25

根据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.

也就是说,返回给定的项x和y,它们是相对于QTableWidget的图形坐标,显然不是您要查找的

您必须使用^{}

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类有一个item()函数,但当您使用self.item语句时,您正在替换该调用,即python读取该语句时,它将使用属性而不是函数,因此您会得到错误:

TypeError 'xxx' object is not callable

相关问题 更多 >