1

How to get your (this, this application currently running) process name in qt?

myWallJSON
  • 9,110
  • 22
  • 78
  • 149

4 Answers4

7

I think you're looking for QCoreApplication::applicationPid().

suszterpatt
  • 8,187
  • 39
  • 60
5

If you want to get the name of the executable (that is, the string normally kept in argv[0]), you might get that by reading the first element of QCoreApplication::arguments().

On Unix, this only works if you initialized your Q(Core)Application correctly:

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    //...
    return app.exec();
}

Edit: Actually, it might be better to call QCoreApplication::applicationFilePath() to get the full path of the executable. Given its full path, you can easily get the name of the executable using QFileInfo::fileName().

mtvec
  • 17,846
  • 5
  • 52
  • 83
  • 2
    Or QFileInfo( QCoreApplication::applicationFilePath() ).fileName() for the filename of the executable. – Frank Osterfeld May 09 '12 at 15:54
  • @Frank: Thanks, I added that to my answer. – mtvec May 09 '12 at 15:59
  • This approach might work for applications that you wrote. But keep in mind that [a process can change it's name during runtime](https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=66443&lngWId=1), even though it is not recommended. So relying on the executable name might not be the best idea. It's best to trust the native API for the job. – karlphillip May 09 '12 at 16:40
3

Unfortunately there's no such thing in Qt.

You'll have to use the native API of the platform you are working with to retrieve this information.

Windows: Check GetCurrentProcessId() and How to get Process Name in C++

Linux: How to get current process name in linux?

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

Use the arg first part

   QStringList args = QCoreApplication::instance()->arguments();
    args.takeFirst(); 
Sherif O.
  • 506
  • 4
  • 15