[PyQt] QShortcut causes seg fault with non-widget context

Robert Kent rob at gulon.co.uk
Thu Oct 30 16:03:23 GMT 2014


Hi All,

So, to answer my own question, this seg fault only occurs when the shortcuts
context is set as a property (i.e. a keyword argument) on instantiation. Its
fine if you do it afterwards via the appropriate setter method, so my little
example program would go as follows:

import sip
sip.setapi('QString',2)
sip.setapi('QVariant',2)

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):
    def __init__(self, parent=None, **kwargs):
        QWidget.__init__(self, parent, **kwargs)

        self.shortcut=QShortcut(
            QKeySequence("Alt+1"),
            self,
            activated=self._sca
        )
        self.shortcut.setContext(Qt.ApplicationShortcut)

    @pyqtSlot()
    def _sca(self): print "SCA"

if __name__=="__main__":
    from sys import argv, exit

    a=QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())

Sorry for the inbox spam folks, Rob

From:  Robert Kent <rob at gulon.co.uk>
Date:  Thursday, 30 October 2014 13:42
To:  pyqt <pyqt at riverbankcomputing.com>
Subject:  [PyQt] QShortcut causes seg fault with non-widget context

Hi All,

I'm trying to use QShortcut to set a global shortcut for my application,
however if I set the context to anything other than Qt.WidgetContext (I.e.
Qt.ApplicationContext in my case) the application crashes. This behaviour is
true for Mac OSX 10.8 and Windows 7. I'm using Python 2.7.2, Qt4.8 and
PyQt4.10. The following little program is a demo and crashes for me:

import sip
sip.setapi('QString',2)
sip.setapi('QVariant',2)

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):
    def __init__(self, parent=None, **kwargs):
        QWidget.__init__(self, parent, **kwargs)

        self.shortcut=QShortcut(
            "Ctrl+P",
            self,
            context=Qt.ApplicationShortcut,
            activated=self._sca
        )

    @pyqtSlot()
    def _sca(self): print "SCA"

if __name__=="__main__":
    from sys import argv, exit

    a=QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())

Just wondering if anybody has had this issue and can tell me what I'm doing
wrong or if this is a genuine bug.

Thanks very much, Rob
_______________________________________________ PyQt mailing list
PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141030/c31a3832/attachment.html>


More information about the PyQt mailing list