Application callbacks


It is possible to register Python callbacks with the application object that Designer will call when specific events happen.

User interface objects, such as menus and buttons, can trigger callbacks using the Qt for Python library. For more information, see Creating user interface elements.


Application callbacks example
import sd

# Our callbacks.
def onBeforeFileLoadedCallback(filePath):
    print("Before file loaded, file: %s" % filePath)

def onAfterFileLoadedCallback(filePath, succeed, updated):
    print("After file loaded, file: %s, succeed: %s, updated: %s" % (filePath, succeed, updated))
    
def onBeforeFileSavedCallback(filePath, parentPackagePath):
    print("Before file saved, file: %s, parentPackage: %s" % (filePath, parentPackagePath))

def onAfterFileSavedCallback(filePath, succeed):
    print("After file saved, file: %s, succeed: %s" % (filePath, succeed))

# Get the application.
app = sd.getContext().getSDApplication()

# Register our callbacks.
beforeFileLoadedCallbackID = app.registerBeforeFileLoadedCallback(onBeforeFileLoadedCallback)
afterFileLoadedCallbackID = app.registerAfterFileLoadedCallback(onAfterFileLoadedCallback)
beforeFileSavedCallbackID = app.registerBeforeFileSavedCallback(onBeforeFileSavedCallback)
afterFileSavedCallbackID = app.registerAfterFileSavedCallback(onAfterFileSavedCallback)

# Unregister callbacks when no longer needed.
app.unregisterCallback(beforeFileLoadedCallbackID)
app.unregisterCallback(afterFileLoadedCallbackID)
app.unregisterCallback(beforeFileSavedCallbackID)
app.unregisterCallback(afterFileSavedCallbackID)