Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the set of all signals for a given widget?

I am looking through the Qt documentation. Is there a quick and dirty way to get a list of all signals that a widget can emit.

For example (withPyQt):

allSignalsList = thisWidget.getSignals()

Alternatively, is there is a nice place on the new Qt5 API that shows all the signals for a given QObject?

like image 766
ADB Avatar asked Feb 01 '15 02:02

ADB


2 Answers

There's no built-in method for listing signals, but normal python object introspection will get the information fairly easily:

from PyQt5 import QtCore, QtWidgets

def get_signals(source):
    cls = source if isinstance(source, type) else type(source)
    signal = type(QtCore.pyqtSignal())
    for subcls in cls.mro():
        clsname = f'{subcls.__module__}.{subcls.__name__}'
        for key, value in sorted(vars(subcls).items()):
            if isinstance(value, signal):
                print(f'{key} [{clsname}]')

get_signals(QtWidgets.QPushButton)

Output:

clicked [PyQt5.QtWidgets.QAbstractButton]
pressed [PyQt5.QtWidgets.QAbstractButton]
released [PyQt5.QtWidgets.QAbstractButton]
toggled [PyQt5.QtWidgets.QAbstractButton]
customContextMenuRequested [PyQt5.QtWidgets.QWidget]
windowIconChanged [PyQt5.QtWidgets.QWidget]
windowIconTextChanged [PyQt5.QtWidgets.QWidget]
windowTitleChanged [PyQt5.QtWidgets.QWidget]
destroyed [PyQt5.QtCore.QObject]
objectNameChanged [PyQt5.QtCore.QObject]

However, it's probably better to learn to use the Qt Documentation. If you go to the page for a Qt class, there's a Contents sidebar on the top-right which has links for the main member types. This usually includes a section for signals, but if it doesn't, you can drill down through the inherited classes until you find one.

So for example, the QPushButton page doesn't show a signals section, but it inherits QAbstractButton, which does have one.

like image 126
ekhumoro Avatar answered Oct 17 '22 02:10

ekhumoro


The trick is to use the QObject's meta object to iterate through the QObject's methods, then pick out the ones that have a signal type.

For example, this code snippet will print out the names of QThread's signals:

QThread thread;
QMetaObject metaObject = thread.staticMetaObject;
for (int i = 0; i < metaObject.methodCount(); i++) {
    QMetaMethod method = metaObject.method(i);
    if (method.methodType() == QMetaMethod::Signal) {
        qDebug() << "Signal: " << method.name();
    }
}

It should be trivial to adapt that to put the QMetaMethods into a QList or any other data structure.

like image 7
MrEricSir Avatar answered Oct 17 '22 02:10

MrEricSir