我需要在运行时获取来自特定QWidget的可用信号列表。
例如,QWidget是QPushButton,我想获得此列表:
"clicked()"
"pressed()"
"released()"
"toggled()"
"destroyed()"
...
你能给我一个提示怎么做吗?谢谢
答案 0 :(得分:5)
这是获取继承树中所有信号的代码。如果你不需要所有这些,很容易适应你的需求:
#include <QStringList>
#include <QApplication>
#include <QPushButton>
#include <QMetaObject>
#include <QMetaMethod>
#include <QDebug>
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QPushButton pushButton;
const QMetaObject* metaObject = pushButton.metaObject();
do {
for (int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i) {
QMetaMethod metaMethod = metaObject->method(i);
if (metaMethod.methodType() == QMetaMethod::Signal)
qDebug() << metaMethod.methodSignature();
}
} while ((metaObject = metaObject->superClass()));
return application.exec();
}
TEMPLATE = app
TARGET = main
QT += widgets
SOURCES += main.cpp
qmake && make && ./main
"pressed()"
"released()"
"clicked(bool)"
"clicked()"
"toggled(bool)"
"windowTitleChanged(QString)"
"windowIconChanged(QIcon)"
"windowIconTextChanged(QString)"
"customContextMenuRequested(QPoint)"
"destroyed(QObject*)"
"destroyed()"
"objectNameChanged(QString)"