Installing event, signal handler function in Python module

Last edited on

The handler function whose name is passed to installEventHandler(), installSignalHandler() and installLazySignalHandler() must reside in the main module.

To overcome this hurdle the handler function can be assigned to the main module:

import names
import mymodule

def main():



import sys
import names
import squish
import test

def install_handlers():
    # Inject desired function into main module:
    sys.modules['__main__'].handler_cellClicked = handler_cellClicked 
    # Install signal handler by name:
    squish.installLazySignalHandler({"type": "QTableWidget", "unnamed": "1", "visible": "1"}, "cellClicked(int, int)", "handler_cellClicked")

    # Inject desired function into main module:
    sys.modules['__main__'].handler_itemChanged = handler_itemChanged
    # Install signal handler by name:
    squish.installSignalHandler(names.foo_QTableWidget, "itemChanged(QTableWidgetItem*)", "handler_itemChanged")

    # installEventHandler() allows registering via function
    # reference, no tricks like the above required:
    squish.installEventHandler("MessageBoxOpened", handler_MessageBoxOpened)

def handler_cellClicked(obj, row, column):
    test.log("cellClicked: " + str(row) + " / " + str(column))

def handler_itemChanged(obj, item):
    test.log("itemChanged: " + str(item.text()))

def handler_MessageBoxOpened(msg):
    test.log("MessageBoxOpened: " + msg)