我的程序有一个表,并且希望能够右键单击该表中的单元格,并产生适当的操作来操纵该单元格(针对不同单元格的不同操作集)。我的测试代码只有一个生成表格的小部件,在视口上安装了一个事件过滤器,以找出右键单击了哪个单元格。
In ProviderRepository.php line 208:
Class 'Spatie\GoogleCalendar\GoogleCalendarServiceProvider' not found
所以我可以找到合适的单元格,以及在菜单上运行exec_的全局位置。但是菜单不会显示在屏幕上。我想知道这是否是Qt错误,其中菜单不会在表中产生,或者我没有正确创建/执行菜单。任何帮助,将不胜感激。谢谢。
答案 0 :(得分:1)
尝试一下:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QAction, QMenu, QTableWidget,
QMainWindow, QTableWidgetItem, QVBoxLayout, )
from PyQt5 import QtCore
class TestRightClickTableWidget(QWidget): #(QMainWindow): # (QWidget): #
def __init__(self):
super().__init__()
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(2)
self.tableWidget.setItem(0, 0, QTableWidgetItem("Cell 1"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("Cell 2"))
self.tableWidget.setItem(1, 0, QTableWidgetItem("Cell 3"))
self.tableWidget.setItem(1, 1, QTableWidgetItem("Cell 4"))
self.tableWidget.setItem(2, 0, QTableWidgetItem("Cell 5"))
self.tableWidget.setItem(2, 1, QTableWidgetItem("Cell 6"))
self.tableWidget.setItem(3, 0, QTableWidgetItem("Cell 7"))
self.tableWidget.setItem(3, 1, QTableWidgetItem("Cell 8"))
### This property holds how the widget shows a context menu
self.tableWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) # +++
### This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu,
### and the user has requested a context menu on the widget.
self.tableWidget.customContextMenuRequested.connect(self.generateMenu) # +++
self.tableWidget.viewport().installEventFilter(self)
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
def eventFilter(self, source, event):
if(event.type() == QtCore.QEvent.MouseButtonPress and
event.buttons() == QtCore.Qt.RightButton and
source is self.tableWidget.viewport()):
item = self.tableWidget.itemAt(event.pos())
print('Global Pos:', event.globalPos())
if item is not None:
print('Table Item:', item.row(), item.column())
self.menu = QMenu(self)
self.menu.addAction(item.text()) #(QAction('test'))
#menu.exec_(event.globalPos())
return super(TestRightClickTableWidget, self).eventFilter(source, event)
### +++
def generateMenu(self, pos):
print("pos======",pos)
self.menu.exec_(self.tableWidget.mapToGlobal(pos)) # +++
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TestRightClickTableWidget()
ex.show()
sys.exit(app.exec_())