Issue
Here is some code that reproduces my 'error' so to speak:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import (QShortcut, QTextEdit, QApplication)
app = QApplication([])
wid = QTextEdit()
wid.show()
short = QShortcut(QKeySequence(Qt.Key_F2), wid)
short.activated.connect(sys.exit)
app.exec()
Hitting F2 , as expected, shuts the program down. If I were to use other keys as an argument, such as QKeySequence(Qt.Key_Control)
or QKeySequence(Qt.Key_Control + Qt.Key_F2)
, nothing happens when I hit said keys upon launching the program
I can't seem to figure out what about them prevents them from triggering the shortcut. From the reading on other SO threads I've gone through about shortcuts, some state that the widget in question might not be focused. I've made sure my widgets were focused , but despite that, the shortcuts don't trigger when I use such keys
Some state that a widget's child's keyPressEvent might eat up the shortcut. This confuses me. I did go through the docs, and shortcutEvents happen to be their own unique individual event. I don't understand why a widget's keyPressEvent handler might choose to handle a shortcutEvent passed to it
I believe it has something to do with the keys I specified for the shortcuts themselves, but I can't quite put my finger on it. Sure, just using F2 as the shortcut IS an option, but I'd really like to allow for the pressing of say, Shift + F2 or Control + F2 as a shortcut, which isn't working for some odd reason
Solution
Changing part of the key sequence to Qt.CTRL
worked for me; so perhaps something wrong with Qt.Key_Control
in the sequence.Qt.SHIFT
doesnt seem to work for QTextEdit
or QLineEdit
but does with other widgets. Perhaps use a QWidget
as a container and use the keysequence on that rather than QTextEdit
widget alone. Otherwise; subclass QTextEdit
and overwrite keyPressEvent
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import (QShortcut, QWidget, QGridLayout, QTextEdit, QApplication)
app = QApplication([])
wid = QWidget()
layout=QGridLayout(wid)
tEdit=QTextEdit()
layout.addWidget(tEdit)
wid.show()
short = QShortcut(QKeySequence(Qt.SHIFT+Qt.Key_P), wid)
#short = QShortcut(QKeySequence(Qt.CTRL+Qt.Key_F2), wid)
#short = QShortcut(QKeySequence(Qt.SHIFT+Qt.Key_1), wid) - doesnt work on textedit/lineedit but works on other widgets
short.activated.connect(sys.exit)
wid.setFocus() #simulate focus on widget
app.exec()
Answered By - dree
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.