X.Anderson
X.Anderson

Reputation: 41

How to get index of a cell in QTableWidget?

I created a table widget and added a contextmenu to it. When I right click the cell,I want to get a file directory and put it into the cell. I've got the directory and pass it to a variable, but i failed to display it in the cell,because I can't get the index of the cell.How to get index of a cell in QTableWidget? Is there any orther method to figure out this qusstion? I'm using Python and 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)

Upvotes: 1

Views: 4160

Answers (1)

X.Anderson
X.Anderson

Reputation: 41

hahaha, I find the mistake!

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

replace these two lines

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

then it works.

Upvotes: 3

Related Questions