在多个 QTableView 上创建不同的上下文菜单
Create different context menus on several QTableViews
我在 QMainWindow
中有两个 QTableView
,我想在其中一个上创建一个上下文菜单,然后在另一个上创建另一个上下文菜单。
创建上下文菜单和定义操作到目前为止有效。但是上下文菜单会在整个应用程序中随处弹出。我不知道如何将它限制为只有一个特定的 table。我认为它与 QMainWindow
的成员 contextMenuEvent()
有关,但我不知道如何更改这部分。尝试创建继承 QTableView
的自定义 class 没有成功,因为我不确定从哪里开始。
这是我尝试过的:
populate_table_1()
和 populate_table_2()
方法仅用于将一些数据填充到 table 中。 get_selected_item_TV1()
方法从 table_1
的一行中获取必要的数据。 delete_file()
方法是从 contextMenuEvent()
方法调用删除操作时我想要执行的操作的示例。这段代码到目前为止有效,但我希望上下文菜单仅在我右键单击一行 table_1
时弹出,并且在其他地方右键单击时它根本不出现。
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
data_1 = ["file_name", "file_infos"]
data_2 = ["other_stuff_1", "other_stuff_2"]
class Ui_MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("MyApp")
self.resize(450, 280)
centralwidget = QWidget(self)
#]===================================================================[#
# table_1
table_1 = QTableView(
centralwidget,
selectionBehavior=QAbstractItemView.SelectRows,
editTriggers=QAbstractItemView.NoEditTriggers
)
# table_1 models
self.modelTV1 = QStandardItemModel(0, 2, centralwidget)
self.modelTV1.setHorizontalHeaderLabels(["column 1", "column 2"])
table_1.setModel(self.modelTV1)
self.selectionModelTV1 = table_1.selectionModel()
#]===================================================================[#
# table_2
table_2 = QTableView(
centralwidget,
selectionBehavior=QAbstractItemView.SelectRows,
editTriggers=QAbstractItemView.NoEditTriggers,
)
# table_2 models
self.modelTV2 = QStandardItemModel(0, 2, centralwidget)
self.modelTV2.setHorizontalHeaderLabels(["column 1", "column 2"])
table_2.setModel(self.modelTV2)
self.selectionModelTV2 = table_2.selectionModel()
v_Layout1 = QVBoxLayout()
v_Layout1.addWidget(table_1)
v_Layout1.addWidget(table_2)
gridLayout = QGridLayout(centralwidget)
gridLayout.addLayout(v_Layout1, 0, 0, 1, 1)
self.setCentralWidget(centralwidget)
def populate_table_1(self):
self.modelTV1.setRowCount(0)
for item in data_1:
self.modelTV1.insertRow(0)
for i, text in enumerate(data_1):
self.modelTV1.setItem(0, i, QStandardItem(text))
def populate_table_2(self):
self.modelTV2.setRowCount(0)
for item in data_2:
self.modelTV2.insertRow(0)
for i, text in enumerate(data_2):
self.modelTV2.setItem(0, i, QStandardItem(text))
def contextMenuEvent(self, event):
self.contextMenu = QMenu(self)
deleteAction = QAction("Delete", self)
self.contextMenu.addAction(deleteAction)
deleteAction.triggered.connect(lambda: self.delete_file(event))
self.contextMenu.popup(QCursor.pos())
def get_selected_item_TV1(self):
# get the row's text from the first column in table_1
listed_items = self.selectionModelTV1.selectedRows()
for index in listed_items:
selected_item = index.data()
return f"table_1 - row_{index.row()} - {selected_item}"
def delete_file(self, event):
item = self.get_selected_item_TV1()
print(f"Deleting: {item}")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mainUI = Ui_MainWindow()
mainUI.populate_table_1()
mainUI.populate_table_2()
mainUI.show()
sys.exit(app.exec_())
有很多选择:
检测鼠标按下时在某个区域,比如第一个QTableView。在这种情况下,小部件必须可以在 contextMenuEvent() 中访问,因此您必须将
table_1
更改为self.table_1
,然后使用 underMouse():def contextMenuEvent(self, event): if self.table_1.underMouse(): self.contextMenu = QMenu(self) deleteAction = QAction("Delete", self) self.contextMenu.addAction(deleteAction) deleteAction.triggered.connect(lambda: self.delete_file(event)) self.contextMenu.popup(QCursor.pos())
为每个QTableView实现contextMenuEvent方法:
class TableView(QTableView): def contextMenuEvent(self, event): self.contextMenu = QMenu(self) deleteAction = QAction("Delete", self) self.contextMenu.addAction(deleteAction) deleteAction.triggered.connect(lambda: self.delete_file(event)) self.contextMenu.popup(QCursor.pos()) def get_selected_item_TV1(self): # get the row's text from the first column in table_1 listed_items = self.selectionModel().selectedRows() for index in listed_items: selected_item = index.data() return f"table_1 - row_{index.row()} - {selected_item}" def delete_file(self, event): item = self.get_selected_item_TV1() print(f"Deleting: {item}")
然后您必须将
table_1 = QTableView(...
更改为table_1 = TableView(...
另一种选择是使用 customContextMenuRequested 信号,为此您必须启用 Qt::CustomContextMenu 标志:
table_1 = QTableView( centralwidget, selectionBehavior=QAbstractItemView.SelectRows, editTriggers=QAbstractItemView.NoEditTriggers, contextMenuPolicy=Qt.CustomContextMenu ) table_1.customContextMenuRequested.connect(self.on_customContextMenuRequested)
def on_customContextMenuRequested(self): self.contextMenu = QMenu(self) deleteAction = QAction("Delete", self) self.contextMenu.addAction(deleteAction) deleteAction.triggered.connect(lambda: self.delete_file()) self.contextMenu.popup(QCursor.pos()) def delete_file(self): item = self.get_selected_item_TV1() print(f"Deleting: {item}")