[PySide] QShortcut suppressed by parent widget?

Alexey Vihorev vihorev at gmail.com
Mon Jul 11 09:13:40 CEST 2016


self.shortcut = QtGui.QShortcut(self, context=QtCore.Qt.WidgetShortcut)

The context. In your case IMO it should be QtCore.Qt.WindowShortcut. More
here <http://doc.qt.io/qt-4.8/qt.html#ShortcutContext-enum>

2016-07-11 9:30 GMT+03:00 Frank Rueter | OHUfx <frank at ohufx.com>:

> Hi all,
>
> below is some test code that shows the problem I'm having, where a
> shortcut assigned to a child widget will not be triggered once it's part of
> the main widget.
> It works if the widget is called by itself but not when it's part of a
> parent widget.
>
> I have been searching the internet for an solution but haven't had any
> luck.
>
> Does anybody know what I'm doing wrong?
>
> Cheers,
> frank
>
> import sys
> from PySide import QtGui, QtCore
>
>
> class MainWidget(QtGui.QWidget):
>
>     def __init__(self, parent=None):
>         super(MainWidget, self).__init__(parent)
>         self.makeUI()
>
>     def makeUI(self):
>         self.layout = QtGui.QHBoxLayout()
>         self.setLayout(self.layout)
>         sw1 = SubWidget('test1')
>         sw2 = SubWidget('test2')
>         sw3 = SubWidget('test3')
>         self.layout.addWidget(sw1)
>         self.layout.addWidget(sw2)
>         self.layout.addWidget(sw3)
>
> class SubWidget(QtGui.QLabel):
>     def __init__(self, text, parent=None):
>         super(SubWidget, self).__init__(parent)
>         self.setText(text)
>         self.shortcut = QtGui.QShortcut(self,
> context=QtCore.Qt.WidgetShortcut)
>         self.shortcut.setKey('i')
>         self.shortcut.activated.connect(self.showInfoWidget)
>
>     def showInfoWidget(self, text):
>         print 'showing info for', self.text()
>
>
> if __name__ == '__main__':
>     app = QtGui.QApplication([])
>     w = MainWidget()
>     w.show()
>     sys.exit(app.exec_())
>
>
>
> --
> [image: ohufxLogo 50x50] <http://www.ohufx.com> *vfx compositing
> <http://ohufx.com/index.php/vfx-compositing> | workflow customisation and
> consulting <http://ohufx.com/index.php/vfx-customising> *
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
>
>


-- 
Алексей Вихорев
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160711/aeea0e3b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ohufxLogo_50x50.png
Type: image/png
Size: 2666 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20160711/aeea0e3b/attachment.png>


More information about the PySide mailing list