Jack
Jack

Reputation: 170

How to get a function's name inside of it

I'm trying to log when something is wrong, so I want to write the class info and function name like this:

void MainWindowTest::testMethod()
{
    qDebug()<<QString("ClassName is:%0,Function Name is:%1")
              .arg(this->metaObject()->className()).arg("how to get method name");
}

How do I do this?

Upvotes: 13

Views: 14218

Answers (2)

张静茹
张静茹

Reputation: 1

QT_STRINGIFY(arg):

The macro can be used to turn the builtin line expander arg into a string literal.

MainWindowVM::MainWindowVM(QObject* parent) : QObject(parent)
{
    QDebug() << QT_STRINGIFY(MainWindowVM);
}

Upvotes: 0

warunanc
warunanc

Reputation: 2261

You can use Q_FUNC_INFO.

Sample code:

qDebug() << "Function Name: " << Q_FUNC_INFO;

Refer to the Qt Documentation — <QtGlobal> - Global Qt Declarations — const char*Q_FUNC_INFO.

Upvotes: 39

Related Questions