Giter Club home page Giter Club logo

Comments (4)

Mr-ZBin avatar Mr-ZBin commented on August 26, 2024 1

Hi @matkuki,

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

Thank you very much

from exco.

matkuki avatar matkuki commented on August 26, 2024

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

from exco.

Mr-ZBin avatar Mr-ZBin commented on August 26, 2024

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

from exco.

matkuki avatar matkuki commented on August 26, 2024
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())

from exco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.