[PyQt] QShortcuts + PyQt5
Vincent Vande Vyvre
vincent.vande.vyvre at telenet.be
Sun Feb 8 09:13:32 GMT 2015
Le 08/02/2015 09:19, Matt Fields a écrit :
> 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
> 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.
> 1. Where am I supposed to put "QShortcut"? Does it go in the Ui file
> 2. How/from where do I import QShortcut class?
> "main"() looks like this:
> app = QtWidgets.QApplication(sys.argv)
> window = QtWidgets.QWidget()
> ui = MyWidget()
> Thanks greatly
> - Matt
> PyQt mailing list PyQt at riverbankcomputing.com
QShortcut is into the module QtWidgets, so use QtWidgets.QShortcut or
from QtWidgets import QShortcut
More information about the PyQt