12

I try to make 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 to do this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jack
  • 161
  • 1
  • 13
  • 20
  • 1
    Well, since you are already in the method, you are able to just put in the method name. Put the string "testMethod". – Min Lin Apr 10 '13 at 03:26
  • 1
    My favorite method is described here: http://stackoverflow.com/questions/173821/how-to-get-the-function-name-while-in-a-function-for-debug-strings – Klathzazt Apr 10 '13 at 04:08

2 Answers2

37

You can use Q_FUNC_INFO

sample code:

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

Refer Qt Documentation

warunanc
  • 2,221
  • 1
  • 23
  • 40
0

QT_STRINGIFY

MainWindowVM::MainWindowVM(QObject* parent) : QObject(parent)
{
    QDebug() << QT_STRINGIFY(MainWindowVM);
}
张静茹
  • 1
  • 2
  • Better to use QT_STRINGIFY becouse is dot not catch method return type –  Jul 18 '23 at 11:06