[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 :
> 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
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
QShortcut is into the module QtWidgets, so use QtWidgets.QShortcut or
from QtWidgets import QShortcut

Vincent


More information about the PyQt mailing list