问题陈述
我试图从我的QtableWidget中选择数据行,并将它们打印到我的控制台上,这样我就可以测试一些东西,最终目标是能够绘制数据。然而,我永远无法抓取整行数据。
背景
我已经制作了一个GUI,它可以通过导入一个特定格式的CSV文件来嵌入多个QTableWidgets。其目标是能够从相同或不同的表中从多个行中提取数据,然后以并排方式绘制它们。其中,每一行数据将是自己的数据集,并有自己的绘图,但在相同的图形上将有多个绘图。
为了完成这一任务,我创建了一个名为CompareWindow的窗口,当按下名为“比较”的Q按钮时,该窗口将打开。该窗口提示用户键入表的名称以及他们希望绘制的表中的相应行。
在提交这些信息之后,我有可以引用的字典,它保存了所有已实例化的QTableObjects。其中键是与其对应的Table对象连接的表的名称。
问题
我尝试获取行数据的两个主要方法是…
第一个想法是使用TableObject.selectRow()命令来迭代我想要的行,但是每当我这样做的时候,它就会返回一个非类型。
我尝试的第二个方法是迭代给定的行列,以便通过追加项值来逐个填充列表。然而,当我这样做时,它只重复地用相同的数字填充列表,这是我的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
这个问题发生在我的文件Perspective.py中的initiateMultiPlot方法中。文件CompareWindow.py向我的Perspective.py发送一个信号,并连接到initateMultiPlot。如有任何问题需要更深入的解释,请询问。
发布于 2017-08-09 01:40:54
根据文档
QTableWidgetItem *QTableWidget::itemAt(int ax,int ay) const 在表小部件的坐标系中,返回相当于QPoint(ax,ay)的位置的项,如果指定的点未被表小部件中的项覆盖,则返回0。
也就是说,返回给定的项目x和y,它们是与QTableWidget
有关的图形坐标,显然不是您要寻找的。
您必须使用item()
QTableWidgetItem *QTableWidget::item(int行,int列) const 如果已设置行和列,则返回给定行和列的项;否则返回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‘对象不可调用
https://stackoverflow.com/questions/45578113
复制相似问题