Pythonclass MyQWidget(QWidget): def __init__(self): QWidget.__init__(self) self.menu = QMenu(self) self.menu.addAction(tr('Copy')) self.menu.triggered.connect(self.copy) def mousePressEvent(self, event): self.parent.__self__.mousePressEvent(event) if event.button() == Qt.RightButton: self.selectAll() self.menu.exec_(event.globalPos()) class MyQLineEdit(QLineEdit, MyQWidget): pass class MyQTextEdit(QTextEdit, MyQWidget): pass
Python def __init__(self) lineEdit = QLineEdit() lineEdit.setContextMenuPolicy(Qt.CustomContextMenu) lineEdit.customContextMenuRequested.connect(self.copy_context_menu) textEdit = QTextEdit() textEdit.setContextMenuPolicy(Qt.CustomContextMenu) textEdit.customContextMenuRequested.connect(self.copy_context_menu) self.copy_all_action = QAction(tr('Copy'), self) self.copy_all_action.triggered.connect(self.copy_action) def copy_context_menu(self, pos): menu = QMenu(self) menu.addAction(self.copy_all_action) menu.exec_(self.mapToGlobal(pos)) def copy_action(self): self.focusWidget().selectAll() self.focusWidget().copy()
Python def copy_context_menu(self, pos): menu = QMenu() menu.addAction(self.copy_all_action) menu.exec_(self.focusWidget().mapToGlobal(pos))
Pythondef copy_context_menu(self, pos): menu = QMenu() menu.addAction(self.copy_all_action) menu.exec_(self.sender().mapToGlobal(pos))
Pythonclass SomeClass(QWidget): def __init__(self) super(SomeClass, self).__init__() self.lineEdit = QLineEdit() self.textEdit = QTextEdit() self.lineEdit.installEventFilter(self) self.textEdit.installEventFilter(self) self.copy_all_action = QAction(self.tr('Copy'), self) self.copy_all_action.triggered.connect(self.copy_action) def eventFilter(self, obj, event): if event.type() == QEvent.ContextMenu: menu = QMenu(self) menu.addAction(self.copy_all_action) menu.exec_(event.globalPos()) return True return False
Pythondef eventFilter(self, obj, event): if obj is someObj and event.type() == someEventType: # some code return False
Pythonclass WikiReport(QDialog): def __init__(self): super(QDialog, self).__init__() self.lineEdit = QLineEdit(self) self.textEdit = QTextEdit(self) self.lineEdit.installEventFilter(self) self.textEdit.installEventFilter(self) self.copy_all_action = QAction(self.tr('Copy All'), self) self.copy_all_action.triggered.connect(self.copy_action) def eventFilter(self, obj, event): print type(obj), type(event) if event.type() == QEvent.ContextMenu: menu = QMenu(self) menu.addAction(self.copy_all_action) menu.exec_(event.globalPos()) return True return False def copy_action(self): self.sender().selectAll() self.sender().copy()
Pythonclass WikiReport(QDialog): def __init__(self): QDialog.__init__(self) self.copy_all_action = QAction(self.tr('Copy All'), self) self.copy_all_action.triggered.connect(self.copy_action) menu = QMenu() menu.addAction(self.copy_all_action) exec_ctx_menu = lambda pos: menu.exec_(self.sender().mapToGlobal(pos)) self.lineEdit = QLineEdit() self.lineEdit.setContextMenuPolicy(Qt.CustomContextMenu) self.lineEdit.customContextMenuRequested.connect(exec_ctx_menu) self.textEdit = QTextEdit() self.textEdit.setContextMenuPolicy(Qt.CustomContextMenu) self.textEdit.customContextMenuRequested.connect(exec_ctx_menu) def copy_action(self): self.focusWidget().selectAll() self.focusWidget().copy()
Pythonself.textEdit.viewport().installEventFilter(self)
Pythonclass WikiReport(QDialog): def __init__(self): super(QDialog, self).__init__() self.lineEdit = QLineEdit(self) self.textEdit = QTextEdit(self) self.lineEdit.installEventFilter(self) self.textEdit.viewport().installEventFilter(self) def eventFilter(self, obj, event): if event.type() == QEvent.ContextMenu: copy_all_action = QAction(self.tr('Copy All'), self) copy_all_action.triggered.connect(lambda: self.copy_action(obj)) menu = QMenu(self) menu.addAction(copy_all_action) menu.exec_(event.globalPos()) return True return False def copy_action(self, obj): obj.selectAll() obj.copy()
Pythondef eventFilter(self, obj, event): if event.type() == QEvent.ContextMenu: if obj is self.textEdit.viewport(): obj = self.textEdit copy_all_action = QAction(self.tr('Copy All'), self) copy_all_action.triggered.connect(lambda: self.copy_action(obj)) menu = QMenu(self) menu.addAction(copy_all_action) menu.exec_(event.globalPos()) return True return False
Python if isinstance(obj.parentWidget(), QTextEdit): copy_lambda = lambda: self.copy_action(obj.parentWidget()) else: copy_lambda = lambda: self.copy_action(obj)