Find signals and slots from any QT widget object.

# -*- coding: utf-8 -*-
##  Copyright 2019 Trevor van Hoof and Jan Pijpers.
##  Licensed under the Apache License, Version 2.0
##  Downloaded from https://janpijpers.com or https://gumroad.com/janpijpers
##  See the license file attached or on https://www.janpijpers.com/script-licenses/

'''
Name: findSignalsAndSlotsQt

This script gives you all available signals and slots on a qt widget object.
Normally you can just check the documentation, however if custom signals and slots are used its hard to find them.
We do this by using the meta class from the object.

I used this to find the timechanged event on the maya timeControl widget.

'''
import sys
from PySide.QtGui import * ## pip install PySide
from PySide import QtCore
def get_widget(name):
    '''
        Kind of slow method of finding a widget by object name.
    :param name:
    :return:
    '''
    for widget in QApplication.allWidgets():
        try:
            if name in widget.objectName():
                return widget
        except Exception as e:
            print e
            pass
    return None 


def test( *arg, **kwarg):
    '''
        Simple test function to see what the signal sends out.
    :param arg:
    :param kwarg:
    :return:
    '''

    print "The args are: ", arg
    print "The kwargs are: ", kwarg
    print 

if __name__ == "__main__":

    ## Here we make a simple QLineEdit for argument sake ...
    app = QApplication(sys.argv)
    wid = QLineEdit()
    wid.setObjectName("myLineEdit")
    wid.show()

    ## Find the widget by name.
    ## See the qt ui list hierarchy script to find all widgets in a qt ui.
    widgetObjectName = "myLineEdit"
    widgetObject = get_widget(widgetObjectName)
    if not widgetObject:
        raise Exception("Could not find widget: %s" %widgetObjectName)

    ## Sanity check
    if not wid == widgetObject:
        raise Exception("Should not happen.XD")

    ## Get the meta data from this object
    meta = widgetObject.metaObject()

    ## Itterate over the number of methods available
    for methodNr in xrange(meta.methodCount()):
        method = meta.method(methodNr)
        ## If the method is a signal type
        if method.methodType() == QtCore.QMetaMethod.MethodType.Signal:
            ## Print the info.
            print
            print "This is the signal name", method.signature()
            print "These are the signal arguments: ", method.parameterNames()
            ## If the method is a signal type
        if method.methodType() == QtCore.QMetaMethod.MethodType.Slot:
            ## Print the info.
            print
            print "This is the slot name", method.signature()
            print "These are the slot arguments: ", method.parameterNames()

    '''
    output example:
    ...
    This is the signal name textChanged(QString)
    These are the signal arguments:  [PySide.QtCore.QByteArray('')]
    
    This is the signal name textEdited(QString)
    These are the signal arguments:  [PySide.QtCore.QByteArray('')]
    ...
    
    so now you can do
    
    widgetObject.textChanged.connect(test)
    
    any every time the text changes the 'test' function will be called 
    
    '''
    widgetObject.textChanged.connect(test)

    sys.exit(app.exec_())

Leave a Reply

Your email address will not be published.