How to get your (this, this application currently running) process name in qt?
-
1Define "process name". Do you mean the file name of the executable? – Frank Osterfeld May 09 '12 at 15:55
4 Answers
I think you're looking for QCoreApplication::applicationPid().

- 8,187
- 39
- 60
-
3PID is not the name. Process ID and Process Name are different attributes. – karlphillip May 09 '12 at 14:47
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()
.

- 17,846
- 5
- 52
- 83
-
2Or QFileInfo( QCoreApplication::applicationFilePath() ).fileName() for the filename of the executable. – Frank Osterfeld May 09 '12 at 15:54
-
-
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
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++

- 1
- 1

- 92,053
- 36
- 243
- 426
Use the arg first part
QStringList args = QCoreApplication::instance()->arguments();
args.takeFirst();

- 506
- 4
- 15