How to get the name of parameters ?
Solved
General and Desktop
-
I need to print the name of parameters.
QStringList player1; player1 << "a" << "a" << "b" << "c"; duplicateCount(player1); void MainWindow::duplicateCount(QStringList stringList) { qDebug() << stringList.name << stringList; }
Output:
player1 ("a" , "a" , "b" , "c") -
Do you want fetch a, b, c etc separately ? What do u mean by get the name of parameters ?Did you check on stringList.at(index) function ?
-
In that case, you can use a macro. like this:
#define PRINT_VAR_NAME(name) printVarName(#name, (name)) void printVarName(const QString &name, const QStringList &list) { qDebug() << name << list; } [...] QStringList player1; player1 << "a" << "a" << "b" << "c"; PRINT_VAR_NAME(player1);
-
@sonichy said in How to get the name of parameters ?:
But I want to print var name: player1.
You cannot do that in C++, because variable names are removed when your code is compiled.
You can pass the name as a separate parameter:
void MainWindow::duplicateCount(const QStringList &stringList, const QString &name) { qDebug() << name << stringList; }
Anyway, why do you need to know the name?
-
@sonichy said in How to get the name of parameters ?:
@JKSH It seems like SLOT needs sender().
Sorry, what do you mean?