How to get cell value from selected row (QTableView)?

可紊 提交于 2020-08-24 07:18:51

问题


I have a QTableView and I need to the get value (string) from the first cell of the selected row (any cell on the row could be selected). But I need this value only if exactly one row was selected.

I thought - I need to get index of the selected row and then get the value of the first сell on that line, but I couldn't find a way to do it.


回答1:


myTableView->selectionModel()->currentIndex().row()

Will give you the index of the currently selected row. From there you should have enough information to look up the row/column pair in your model.

Also, QItemSelectionModel::selectedRows() will let you know how many rows are selected.




回答2:


Python Code will look like :

    self.tableView.clicked.connect(self.on_Click)

When User Click on Table Cell the on_Click() method is invoked

    def on_Click(self):
        # #selected cell value.
        index=(self.tableView.selectionModel().currentIndex())
        # print(index)
        value=index.sibling(index.row(),index.column()).data()
        print(value)

Explanation.

"value" contains the cell value of the selected cell.

       index.row() # gives current selected row.
       index.column() # gives current selected column.
       index.sibling(index.row(),index.column()).data() # will return cell data


来源:https://stackoverflow.com/questions/11710705/how-to-get-cell-value-from-selected-row-qtableview

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