/ Coding-Sucks

列出 QObject 的成員函數

把要知道成員函數列表的物件放在 targetObject 的位置,執行:

QObject* targetObject;
const QMetaObject* mo = targetObject->metaObject();
for(int i=0; i<mo->methodCount(); ++i) {
    QMetaMethod mm = mo->method(i);

    QString typeText;
    switch (mm.methodType()) {
    case QMetaMethod::Method:
        typeText = "method";
        break;
    case QMetaMethod::Signal:
        typeText = "signal";
        break;
    case QMetaMethod::Slot:
        typeText = "slot";
        break;
    case QMetaMethod::Constructor:
        typeText = "constructor";
        break;
    default:
        typeText = "";
    }

    QString accessText;
    switch (mm.access()) {
    case QMetaMethod::Private:
        typeText = "private";
        break;
    case QMetaMethod::Protected:
        typeText = "protected";
        break;
    case QMetaMethod::Public:
        typeText = "public";
        break;
    default:
        typeText = "";
    }

    qDebug() << accessText << typeText << mm.methodSignature();
}

執行後會印出 targetObject 全部的成員函數。例如將 targetObject 設為一個 QObject 物件,可得到:

"" "public" "destroyed(QObject*)"
"" "public" "destroyed()"
"" "public" "objectNameChanged(QString)"