Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [solved] PySide/PyQT4: Catching SIGUSR1 to trigger a Signal

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

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 5.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cshapeshifter
    wrote on last edited by
    #1

    Hi there,

    I'm really stuck with this one. I read http://doc.qt.nokia.com/4.7-snapshot/unix-signals.html 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...")
    self.cw = QtGui.QWidget(self)
    self.setCentralWidget(self.cw)
    # 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):
        print("OK!")
        self.setWindowTitle("OK!")
    

    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):
    sys.exit(0)
    signal.signal(signal.SIGINT, sigint_handler)

    create Qt GUI

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

    run Qt main loop

    r = app.exec_()
    sys.exit(r)

    What happens:

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

    shapeshifter@Tachychineta> python2 sigusr_to_qt.py

    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]

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cshapeshifter
        wrote on last edited by
        #3

        Thanks a lot, it works!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved