[英]How to get index of a cell in QTableWidget?
我創建了一個表格小部件並為其添加了一個上下文菜單。 右鍵單擊單元格時,我想獲取一個文件目錄並將其放入單元格中。 我已經有了目錄並將其傳遞給變量,但是由於無法獲取該單元的索引,所以無法在該單元格中顯示它。如何在QTableWidget中獲取一個單元的索引? 有沒有其他方法可以解決這個問題? 我正在使用Python和PyQt5。
@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()
然后就可以了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.