Can't select entire row of data from QTableWidget

。_饼干妹妹 提交于 2021-02-13 17:32:10

问题


Problem Statement

I'm trying to select rows of data from my QtableWidget and print them out to my console just so I can test some things, with the end goal being able to plot the data. However I can never grab the whole row of data.

Background

I have made a GUI that can embed several QTableWidgets by importing a specifically formatted CSV file. The goal is to be able to pull data from multiple rows from the same or different tables and then plot them in a side by side fashion. Where each row of data will be its own dataset and have its own plot, but there will be multiple plots on the same figure.

To complete this task I have made a window called CompareWindow that opens when a Qpushbutton called "Compare" is pressed. The window prompts the user to type in the names of the tables and the respective rows from that table they wish to plot.

After this information is submitted I have dictionary that I can reference which has saved all the QTableObjects that have been instantiated. Where the keys are the names given to the tables which are connected to their corresponding Table Object.

Problem

The two main methods I have tried to grab the row data are…

The first idea was using TableObject.selectRow() command I would iterate through the rows I wanted, but whenever I did this to it would return a nonetype.

The second method I tried was to iterate a given rows columns so it would fill a list one by one by appending the item values. However when I did this it only filled the list with the same number repeatedly, which was the first cell in my Qtable.

Even when I explicitly called a certain row or column I would get the same output. The output being pulled is .12, the number from the first cell in my CSV file.

Here is the code in question I'm having problems with.

    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)

Here is my GitHub link containing all my files: https://github.com/Silvuurleaf/Data-Visualize-Project

The problem occurs in my file Perspective.py in the method initiateMultiPlot. The file CompareWindow.py sends a signal to my Perspective.py and is connected to initateMultiPlot. Please inquire if anything requires more in depth explanation.


回答1:


According to the 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.

That is, returns the given item x and y which are graphical coordinates with respect to QTableWidget, and clearly is not what you are looking for.

You must use the 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.

But in your case will not work unless you do the following change:

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)

to:

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)

That is, you change your self.item to item.

The problem is that at first glance the error is rather difficult, the QTableWidget class has an item() function, but when you use the self.item statement you are replacing that call, ie when python reads that statement it will use the attribute and not the function , So you get the error:

TypeError 'xxx' object is not callable



来源:https://stackoverflow.com/questions/45578113/cant-select-entire-row-of-data-from-qtablewidget

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