我有一个QTableView
,我需要从所选行的第一个单元格获取值(字符串)(可以选择行上的任何单元格).但是,只有在选择了一行时才需要此值.
我想 - 我需要得到所选行的索引,然后得到该行上第一个сell的值,但我找不到办法.
Chr*_*ris 11
myTableView->selectionModel()->currentIndex().row()
Run Code Online (Sandbox Code Playgroud)
将为您提供当前所选行的索引.从那里你应该有足够的信息来查找模型中的行/列对.
此外,QItemSelectionModel::selectedRows()
还会告诉您选择了多少行.
小智 7
Python 代码将如下所示:
self.tableView.clicked.connect(self.on_Click)
Run Code Online (Sandbox Code Playgroud)
当用户点击表格单元格时 on_Click() 方法被调用
def on_Click(self):
# #selected cell value.
index=(self.tableView.selectionModel().currentIndex())
# print(index)
value=index.sibling(index.row(),index.column()).data()
print(value)
Run Code Online (Sandbox Code Playgroud)
解释。
“值”包含所选单元格的单元格值。
index.row() # gives current selected row.
index.column() # gives current selected column.
index.sibling(index.row(),index.column()).data() # will return cell data
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10385 次 |
最近记录: |