How to detect doubleClick in QTableView

不问归期 提交于 2020-04-10 08:35:59

问题


I'm using PyQt to create a GUI application. In a view inherited from QTableView, need to detect the row the user has selected when they double click a row. The table has sorting, but no editing.

How do I do it?

Note - tried the doubleClicked(int) signal. It is emitted by mouse buttons, not by data cells, so it was never fired. :(

Ian


回答1:


I dont understand. The doubleClicked signal of the QTableView has the signature

void doubleClicked ( const QModelIndex & index )

If you connect that signal you should obtain the correct QModelIndex.




回答2:


No need to use SIGNALs anymore:

self.your_table.doubleClicked.connect(your_function)

"doubleClicked" being inherited from QAbstractItemView.




回答3:


Once you have the modelIndex, (from Frank's comment above) you can use it to find which cell was double clicked.

def slotDoubleClicked(self, mi):
    row = mi.row()
    column = mi.column()

You then can use these row and col values to access the table with table.setItem(row, column, newdata) or other table method




回答4:


Like @regomodo said, you can simply connect your function to the double click via:

self.your_table.doubleClicked.connect(your_function)

Then, if you want to know on which row the user double clicked, you can use the following code:

for idx in self.your_table.selectionModel().selectedIndexes():
        row_number = idx.row()
        column_number = idx.column()

It will return an integer corresponding to the row or the column number. There will always only be a single value as the double click remove the previous selection.

If you link your function to a push button or another signal, you can receive a list containing multiple elements selected by the user.

For example, you can easily retrieve a list of all selected rows using this code:

rows = []
for idx in self.your_table.selectionModel().selectedIndexes():
    rows.append(idx.row())
rows = list(set(rows))

This will return a list of all selected rows (The set function will also remove any duplicates).

Cheers!



来源:https://stackoverflow.com/questions/4324005/how-to-detect-doubleclick-in-qtableview

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