Please nominate your Qt Champions for 2021!

[solved] PySide/PyQT4: Catching SIGUSR1 to trigger a Signal

  • Hi there,

    I'm really stuck with this one. I read and various Python socket examples and so I came up with this test case, but it just doesn't work. There's a simple window and upon SIGUSR, I want its title to change so I made a socketpair, gave one end to a QSocketNotifier and used the other end to write something to it upon SIGUSR1:

    #!/usr/bin/env python

    -- coding: utf-8 --

    import sys
    import signal
    from PySide import QtCore, QtGui

    A Window which should have its title changed upon SIGUSR1

    class GUI(QtGui.QMainWindow):
    def init(self, rsock, parent=None):
    QtGui.QMainWindow.init(self, parent)
    self.setWindowTitle("Pending...") = QtGui.QWidget(self)
    # setup notifier using one of the sockets
    notifier = QtCore.QSocketNotifier(rsock, QtCore.QSocketNotifier.Read)
    self.connect(notifier, QtCore.SIGNAL("activated(int)"), self.test)
    print("GUI is set up")

    def test(self):

    create socket pair

    import socket
    rsock, wsock = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)

    handler for SIGUSR1

    def sigusr1_handler(signum, stack):
    print 'Received Signal:', signum
    wsock.send("go go go")
    #~ print "sent go"
    #~ print(rsock.recv(10))
    #~ print "end of recv"
    signal.signal(signal.SIGUSR1, sigusr1_handler)

    handler for SIGINT

    def sigint_handler(signum, stack):
    signal.signal(signal.SIGINT, sigint_handler)

    create Qt GUI

    app = QtGui.QApplication(sys.argv)
    timer = QtCore.QTimer()
    timer.timeout.connect(lambda: None)
    win = GUI(rsock)

    run Qt main loop

    r = app.exec_()

    What happens:

    In one terminal, running the test case, the title never changes

    shapeshifter@Tachychineta> python2

    GUI is set up

    Received Signal: 10

    Received Signal: 10

    Received Signal: 10

    The other terminal, sending SIGUSR1

    shapeshifter@Tachychineta> ps -A | grep python

    3035 pts/1 00:00:00 python2

    shapeshifter@Tachychineta> kill -s USR1 3035

    shapeshifter@Tachychineta> kill -s USR1 3035

    shapeshifter@Tachychineta> kill -s USR1 3035

    shapeshifter@Tachychineta> kill -s USR1 3035


    I tried everything and I'm not getting anywhere. Any help, please?
    Thank you.

    [EDIT: code formatting, please use @-tags, not "code", Volker]

  • try keep a reference of "notifier" created on line 16.

    This is necessary because the QSocketNotifier will die in the end of the class constructor.

    Yes, I did the test and works if you keep the notifier live

  • Thanks a lot, it works!

Log in to reply