如何获取 QTableWidget 中单元格的索引?

How to get index of a cell in QTableWidget?

我创建了一个 table 小部件并向其添加了上下文菜单。当我右键单击单元格时,我想获取一个文件目录并将其放入单元格中。我已经得到目录并将它传递给一个变量,但是我无法在单元格中显示它,因为我无法获取 cell.How 的索引来获取 QTableWidget 中单元格的索引?有没有其他方法可以解决这个问题?我正在使用 Python 和 PyQt5。

enter image description here

@pyqtSlot()
def on_actionAddFolder_triggered(self):
    # TODO: Open filedialog and get directory
    filedir = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
    return filedir

@pyqtSlot(QPoint)
def on_tableWidget_customContextMenuRequested(self, pos):
    # TODO: get directory and display it in the cell
    x = self.tableWidget.currentRow
    y = self.tableWidget.currentColumn

    RightClickMenu = QMenu()
    AddFolder = RightClickMenu.addAction('Add Folder')
    FolderAction = RightClickMenu.exec_(self.tableWidget.mapToGlobal(pos))
    if FolderAction == AddFolder:
        NewItem = QTableWidgetItem(self.on_actionAddFolder_triggered())
        self.tableWidget.setItem(x,y, NewItem)

哈哈哈,我找错了!

x = self.tableWidget.currentRow
y = self.tableWidget.currentColumn

替换这两行

x = self.tableWidget.currentRow()
y = self.tableWidget.currentColumn()

然后就可以了。