Home > Blockchain >  How convert sprintf to QString in QtCreator
How convert sprintf to QString in QtCreator

Time:06-08

This line i need covert to QString

sprintf (comando, "/bin/ps -fu %s", getenv ("USER"));

I have it like this but it doesn't show me the results

QString procesos= QString("procesos %1 %2 %3").arg(comando).arg("/bin/ps -fu %s").arg(getenv ("USER"));

And I am printing like this in Qt

ui->label_11->setText(procesos);

CodePudding user response:

Those actions are different, sprintf does not do what you show in the following code snippet.

.arg can't have format specifiers. Perhaps you want

QString procesos = QString("procesos %1").arg(
    QString("/bin/ps -fu %1").arg(getenv("USER")));

or

QString procesos = QString("procesos %1").arg(
    QString().asprintf("/bin/ps -fu %s", getenv("USER")));

or

QString procesos = QString("procesos /bin/ps -fu %1").arg(getenv("USER"));

CodePudding user response:

I think your original attempt was close to correct. The only problem was you tried inserting a %s into the second argument, which isn't recognized in QString, and it's not even necessary. You already had the %3 which takes in the third argument.

I haven't tried it myself, but I believe simply removing the %s should work:

QString procesos= QString("procesos %1 %2 %3").arg(comando).arg("/bin/ps -fu ").arg(getenv ("USER"));
  • Related