@Bonnie said in How to catch a paste action from a widget other than Text container:
You can look into the qt source file about how text containers handle a paste action:
https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qwidgettextcontrol.cpp.html#1313
Following this example, I got a solution.
I have a QAction added in the QMainWindow, with a shortcut being QKeySequence::Paste and connected to a paste function. I have edited this function to detect the context of the paste action. Some extracts of my program in Python:
self.actionPaste = QAction(
QIcon.fromTheme('edit-paste',
QIcon(rsrcPath + '/editpaste.png')),
self.tr("&Paste"), self, priority=QAction.LowPriority,
shortcut=QKeySequence.Paste,
enabled=((len(QApplication.clipboard().text()) != 0) or QApplication.clipboard().mimeData().hasImage()))
tb.addAction(self.actionPaste)
and
def paste(self):
# send the command to the active section
widget = self.focusWidget()
if widget.metaObject().className() == "CTextEdit":
widget.paste()
if widget.metaObject().className() == "ChapterTab":
widget.edit.pasteDrawing()