Skip to content
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

Closed
Mr-ZBin opened this issue Jul 21, 2023 · 4 comments
Closed

QAction setShortcut in QsciScintilla custom menu doesn't work #71

Mr-ZBin opened this issue Jul 21, 2023 · 4 comments

Comments

@Mr-ZBin
Copy link

Mr-ZBin commented Jul 21, 2023

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.

import sys

from PyQt6.Qsci import QsciScintilla
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction
from PyQt6.QtWidgets import QMenu, QApplication


# 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_action = QAction("Refresh File", self)
        self.refresh_file_action.setShortcut("F5")
        self.refresh_file_action.triggered.connect(lambda: print(self.refresh_file_action.text()))

        self.menu = QMenu(self)
        self.menu.addAction(self.refresh_file_action)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    editor = Editor()
    editor.show()
    sys.exit(app.exec())
@matkuki
Copy link
Owner

matkuki commented Jul 21, 2023

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())

@Mr-ZBin
Copy link
Author

Mr-ZBin commented Jul 21, 2023

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

@matkuki
Copy link
Owner

matkuki commented Jul 21, 2023

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())

@Mr-ZBin
Copy link
Author

Mr-ZBin commented Jul 23, 2023

Hi @matkuki,

I think the importance to the key shortcut is self.addAction(action), and it works now.

Thank you very much

@Mr-ZBin Mr-ZBin closed this as completed Jul 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants