gpt4 book ai didi

c++ - 如何获取类函数名称列表? Qt C++

转载 作者:行者123 更新时间:2023-11-30 02:44:35 29 4
gpt4 key购买 nike

例如我有这段代码:

class MyClass : public QObject
{
Q_OBJECT
public:
void MyClass()
{
QStringList selfFunctionList;
//Get functions list
qDebug()<<selfFunctionList;
}
void function1(){}
void function435(){}
};

如何设置“function1”和“function435”的 selfFunctionList 列表?

最佳答案

您可以为此使用staticMetaObject

class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject *parent = 0) :
QObject(parent)
{
for (int n = 0; n < staticMetaObject.methodCount(); n++) {
functions.append(QString::fromLocal8Bit(staticMetaObject.method(n).name()));
}
qDebug() << functions;
}

signals:
void testSignal();

private slots:
void privateTestFunction() {}

public slots:
void publicTestFunction() {}

private:
QStringList functions;
};

一个条件,函数需要声明为槽或信号。

输出:

("destroyed","destroyed","objectNameChanged","deleteLater","_q_reregisterTimers",
"testSignal","privateTestFunction","publicTestFunction")

关于c++ - 如何获取类函数名称列表? Qt C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25166855/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com