从 QTableView 中获取每个单元格的数据

4

我需要从 QtableView 中获取值,但是我不知道如何在没有来自表格的信号的情况下实现。

该表格从 txt 文件中获取其值。 我想从表格中使用这些值,但不想直接在表格中操作。 表格只是一个缓冲区。 那么,如何在没有来自表格本身的任何信号的情况下,“获取”表格中的所有值,仅通过按下 QPushButton 来实现?

1个回答

14

QTableView只是展示其模型中所包含的数据,您需要使用该模型来检索数据。您还必须定义如何存储值。例如:

model = tableView.model()
data = []
for row in range(model.rowCount()):
  data.append([])
  for column in range(model.columnCount()):
    index = model.index(row, column)
    # We suppose data are strings
    data[row].append(str(model.data(index).toString()))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接