QShortcuts catch external shortcuts from ReadOnly textedit
-
Just a quick question: I am declaring manual shortcuts in a widget
copy_shortcut = QShortcut(QKeySequence.Copy, self) copy_shortcut.activated.connect(self.copy)
and I noticed that this also catches those performed when I'm focused on a text edit which is ReadOnly. Which isn't what I want, I want the focused widget to process the event. It works when the text edit is editable, but doesn't when its readonly. I also tried calling
textedit.setFocusPolicy(Qt.ClickFocus)
which didn't help. How can I do this?
Thanks -
Hi,
Can you provide a full minimal script that shows that behaviour ?
From your snippet it's not clear how you are setting everything up. -
Hi,
Can you provide a full minimal script that shows that behaviour ?
From your snippet it's not clear how you are setting everything up.from PySide2.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout, QPlainTextEdit, QShortcut, QPushButton from PySide2.QtGui import QKeySequence, QGuiApplication import sys class CatchingWidget(QWidget): def __init__(self, parent): super().__init__(parent) copy_shortcut = QShortcut(QKeySequence.Copy, self) copy_shortcut.activated.connect(self.copy) self.setLayout(QVBoxLayout()) self.layout().addWidget(QPushButton()) def copy(self): print('copy in widget!') QGuiApplication.clipboard().setText(self.hasFocus()) class MainWindow(QMainWindow): def __init__(self): super().__init__() l = QVBoxLayout() l.addWidget(CatchingWidget(self)) textedit = QPlainTextEdit('some text I would like to copy', self) textedit.setReadOnly(True) l.addWidget(textedit) w = QWidget() w.setLayout(l) self.setCentralWidget(w) if __name__ == '__main__': app = QApplication() mw = MainWindow() mw.show() sys.exit(app.exec_())
This has a widget at the top, the textedit below. If you try to copy text from it using ctrl+c, still the upper widget's copy method is triggered instead, even if it doesn't have focus.
-
this is still really annoying, is this a bug or am I just missing sth? I didn't find anything regarding this in the docs so far. Also found this old bugreport
-
What if you set the shortcut context to
Qt.WidgetWithChildrenShortcut
?