创建上下文菜单来删除QTableView Python内部的行,可以通过以下步骤实现:
from PyQt5.QtWidgets import QApplication, QTableView, QMenu, QAction
from PyQt5.QtCore import Qt
app = QApplication([])
table_view = QTableView()
window.setCentralWidget(table_view)
class CustomContextMenu(QMenu):
def __init__(self, parent=None):
super().__init__(parent)
self.delete_action = QAction("删除行", self)
self.delete_action.triggered.connect(self.delete_selected_row)
self.addAction(self.delete_action)
def delete_selected_row(self):
table = self.parentWidget()
model = table.model()
selection_model = table.selectionModel()
selected_indexes = selection_model.selectedIndexes()
rows = set()
for index in selected_indexes:
rows.add(index.row())
for row in sorted(rows, reverse=True):
model.removeRow(row)
table_view.setContextMenuPolicy(Qt.CustomContextMenu)
context_menu = CustomContextMenu(table_view)
table_view.customContextMenuRequested.connect(context_menu.exec_)
完整代码示例:
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QMenu, QAction
from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex
class CustomTableModel(QAbstractTableModel):
def __init__(self, data, headers):
super().__init__()
self._data = data
self._headers = headers
def rowCount(self, parent=QModelIndex()):
return len(self._data)
def columnCount(self, parent=QModelIndex()):
return len(self._headers)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self._headers[section]
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
row = index.row()
col = index.column()
return self._data[row][col]
def removeRow(self, position, parent=QModelIndex()):
self.beginRemoveRows(parent, position, position)
del self._data[position]
self.endRemoveRows()
class CustomContextMenu(QMenu):
def __init__(self, parent=None):
super().__init__(parent)
self.delete_action = QAction("删除行", self)
self.delete_action.triggered.connect(self.delete_selected_row)
self.addAction(self.delete_action)
def delete_selected_row(self):
table = self.parentWidget()
model = table.model()
selection_model = table.selectionModel()
selected_indexes = selection_model.selectedIndexes()
rows = set()
for index in selected_indexes:
rows.add(index.row())
for row in sorted(rows, reverse=True):
model.removeRow(row)
if __name__ == '__main__':
app = QApplication([])
window = QMainWindow()
window.setWindowTitle("QTableView 示例")
# 模拟数据和表头
data = [
['Alice', '25', 'Female'],
['Bob', '30', 'Male'],
['Charlie', '35', 'Male'],
['Diana', '28', 'Female']
]
headers = ['Name', 'Age', 'Gender']
# 创建表格模型和视图
model = CustomTableModel(data, headers)
table_view = QTableView()
table_view.setModel(model)
window.setCentralWidget(table_view)
# 创建右键菜单
table_view.setContextMenuPolicy(Qt.CustomContextMenu)
context_menu = CustomContextMenu(table_view)
table_view.customContextMenuRequested.connect(context_menu.exec_)
window.show()
app.exec_()
这样,当在QTableView中选中一行或多行,右键点击时会弹出自定义的上下文菜单,其中的"删除行"选项可以删除所选中的行。
推荐的腾讯云相关产品和产品介绍链接地址:
请注意,以上腾讯云产品仅作为示例,并非对其他云计算品牌商的比较或推荐。
领取专属 10元无门槛券
手把手带您无忧上云