Python 如何检索 QTableView 的选定行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22577327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:12:05  来源:igfitidea点击:

how to retrieve the selected row of a QTableView?

pythonpython-2.7pyqtpyqt4qtableview

提问by Haussem Ch?dly

I'm using a QTableView object types. This object contains several online and I use this model:

我正在使用 QTableView 对象类型。这个对象包含几个在线,我使用这个模型:

class PaletteTableModel(QtCore.QAbstractTableModel):
    def __init__(self,colors = [[]],headers =[],parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self.__colors=colors
        self._headers=headers

    def rowCount(self,parent): 
        return len(self.__colors)

    def columnCount(self,parent):
        return 6        

    def headerData(self,section,orientation,role):
        if role==QtCore.Qt.DisplayRole:
            if orientation==QtCore.Qt.Horizontal:
                return self._headers[section]  
            else:
                return QtCore.QString("Credit %1").arg(section+1) 

    def data(self,index,role):
        if role==QtCore.Qt.ToolTipRole:
            row=index.row
            return "Crédit"
        if role==QtCore.Qt.EditRole:
            row=index.row()
            column=index.column()
            return self.__colors[row][column]
        if role==QtCore.Qt.DisplayRole:
            row=index.row()
            column=index.column()
            value=self.__colors[row][column]
            return value  

    def setData(self,index,value,role=QtCore.Qt.EditRole):
        if role==QtCore.Qt.EditRole:
            row =index.row()
            column=index.column()
            ch=(value)

            self.__colors[row][column]=ch
            self.dataChanged.emit(index,index)
            return True

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsSelectable

i want to retrieve the selected row of this tableView..anyone please help me and I want to have a python code if possible

我想检索此 tableView 的选定行..任何人请帮助我,如果可能的话,我想要一个 python 代码

回答by thecreator232

One of the way's you can use to retrieve the selected rows is:

您可以用来检索所选行的方法之一是:

tableView = QtGui.QTableView()
tableModel = PaletteTableModel()   
tableView.setModel(tableModel)

x = tableView.selectedIndexes ()

"tableView.selectedIndexes ()" returns a list-of-QModelIndex's

“tableView.selectedIndexes()”返回一个QModelIndex的列表

回答by ekhumoro

It depends what you mean by "the selected row". By default, a QTableViewhas its selection modeset to ExtendedSelection, and its selection behaviorset to SelectItems. This means that several individual table cells in different rows and columns can be selected at the same time. So which one should count as "the" selected row?

这取决于您所说的“所选行”是什么意思。默认情况下, aQTableView选择模式设置为ExtendedSelection,其选择行为设置为SelectItems。这意味着可以同时选择不同行和列中的多个单独表格单元格。那么哪一个应该算作“选定的”行?

The selection modelof the table-view has a selectedRowsmethod which will return a list of indexes for the rows where all the columsare selected (i.e. as it is when you click on the header section for a row):

表视图的选择模型有一个selectedRows方法,该方法将返回选择所有列的行的索引列表(即当您单击行的标题部分时):

    indexes = table.selectionModel().selectedRows()
    for index in sorted(indexes):
        print('Row %d is selected' % index.row())

However, if you want to get all the rows where at least onecell is selected, you can use the selectedIndexesmethod:

但是,如果要获取至少选择了一个单元格的所有行,则可以使用selectedIndexes方法:

    rows = sorted(set(index.row() for index in
                      self.table.selectedIndexes()))
    for row in rows:
        print('Row %d is selected' % row)

回答by GSandro_Strongs

In my case I use this action into a function that is called in doubleClicked event something like this: I add this line code into the init function

在我的情况下,我将此操作用于在 doubleClicked 事件中调用的函数,如下所示:我将此行代码添加到 init 函数中

self.tableSusAmigos.doubleClicked.connect(self.doubleClicked_table)

After that I declared doubleClicked_table like this:

之后,我像这样声明了 doubleClicked_table:

def doubleClicked_table(self):
    index = self.tableSusAmigos.selectedIndexes()[0]
    id_us = int(self.tableSusAmigos.model().data(index).toString())
    print ("index : " + str(id_us)) 

In this case I show an id (integer) that it's in the first column (that's the reason the number 0 in selectedIndexes()[0])

在这种情况下,我显示一个 id(整数),它位于第一列(这就是 selectedIndexes()[0] 中的数字 0 的原因)