-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QAction setShortcut in QsciScintilla custom menu doesn't work #71
Comments
Hey @Mr-ZBin Try this: import sys
from PyQt6.Qsci import QsciScintilla
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction, QShortcut
from PyQt6.QtWidgets import QMenuBar, QMenu, QApplication
from PyQt6.QtCore import QPoint, QTimer
# Using QTextEdit also can not setShortCut
class Editor(QsciScintilla):
def __init__(self):
super().__init__()
self.initMenu()
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(lambda pos: self.menu.popup(self.mapToGlobal(pos)))
def initMenu(self):
self.refresh_file_shortcut = QShortcut("F5", self)
self.refresh_file_shortcut.activated.connect(lambda: print("Shortcut fired"))
if __name__ == '__main__':
app = QApplication(sys.argv)
editor = Editor()
editor.show()
sys.exit(app.exec()) |
Hi @matkuki, Thanks for your quickly response. But there is no self.menu in initMenu() function. There is no right click menu in your code. Thank you |
import sys
from PyQt6.Qsci import QsciScintilla
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction, QShortcut
from PyQt6.QtWidgets import *
from PyQt6.QtCore import QPoint, Qt
# Using QTextEdit also can not setShortCut
class Editor(QsciScintilla):
def __init__(self):
super().__init__()
self.initMenu()
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(lambda pos: self.menu.popup(self.mapToGlobal(pos)))
def initMenu(self):
self.menu = QMenu(self)
refresh_file_action = QAction("Refresh File", self)
refresh_file_action.setShortcut("F5")
refresh_file_action.setShortcutContext(Qt.ShortcutContext.ApplicationShortcut)
refresh_file_action.setShortcutVisibleInContextMenu(True)
refresh_file_action.triggered.connect(lambda: print(refresh_file_action.text()))
self.menu.addAction(refresh_file_action)
self.addAction(refresh_file_action)
if __name__ == '__main__':
app = QApplication(sys.argv)
editor = Editor()
editor.show()
sys.exit(app.exec()) |
Hi @matkuki, I think the importance to the key shortcut is self.addAction(action), and it works now. Thank you very much |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @matkuki,
This is not a issue but a question. I tried to custom a menu in the QsciScintilla, and assign a key shortcut for the action as below demo code, but the key shortcut "F5" always doesn't work. I google and bing and stackoverflow the question, but doesn't find any useful information. Do you know any information for the key shortcut? Thank you very much.
The text was updated successfully, but these errors were encountered: