【发布时间】:2020-07-21 05:45:36
【问题描述】:
我正在编写一个分析 CSV 文件的 GUI,并且我想实现一个功能,其中只有在选择整行时才会删除该行。我当前的问题是当我选择一个单元格并按退格键时,所选单元格所在的行也将被删除。如何防止这种情况发生?
from GUI import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import Qt
from PyQt5.QtCore import QItemSelectionModel
from PyQt5.QtWidgets import (QApplication,QMainWindow,QFileDialog,QTableWidget,
QMessageBox,QTableWidgetItem,QHeaderView)
class mainForm(QMainWindow,Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.initUI()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Backspace:
self.removeRow()
def selectedRow(self):
if self.tabWidget.currentIndex() is 0 and self.inLoanTable.selectionModel().hasSelection():
row = self.inLoanTable.selectionModel().selectedIndexes()[0].row()
return int(row)
def removeRow(self):
if self.tabWidget.currentIndex() is 0 and self.inLoanTable.rowCount() > 0:
row = self.selectedRow()
【问题讨论】:
-
然后在 keyPressEvents 添加更多 if 在调用 removeRow 之前验证是否选择了整行。
-
如何验证整行是否被选中?
-
这是你要完成的调查部分。 Stackoverflow 不是让社区为您搜索 Google 的最佳场所。
标签: python pyqt5 qtablewidget