[PyQt] QShortcuts + PyQt5

Matt Fields mattfields.china at gmail.com
Sun Feb 8 08:19:44 GMT 2015


Hi.
Can any kindly soul help me grok the QShortcuts with PyQt5?  I'd like to
make a keyboard shortcut to a Python function (not a QMenu action). I'm
really not getting this. All the tutorials and manuals I've found are
PyQt4, and as such do not work. The C++ documentation is not helping me get
any closer.

1. I have a PyQt5 widget, for which I'd like to run "myfunction" after a
keyboard shortcut.

2. In the UI file, call it "MyWidget_Ui.py", the main class we'll call
"SearchWidget".

3. in SearchWidget "__init__", I have something like "self.shortcut =
QShortcut(QKeySequence("Ctrl+O"), self)".

Execution results in error:
self.shortcut = QShortcut(QKeySequence("Ctrl+O"), self)
NameError: name 'QShortcut' is not defined

I'm very confused by the "app" and "window" and "ui" separation. I'm not
clear which one is supposed to get the QShortcut.

Questions:
1. Where am I supposed to put "QShortcut"?  Does it go in the Ui file
__init__?
2. How/from where do I import QShortcut class?


"main"() looks like this:
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()

    ui = MyWidget()
    ui.setupUi(window)

    window.show()

Thanks greatly
- Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150208/d9dd72be/attachment.html>


More information about the PyQt mailing list