How to Get Current User Name in Qt/C++ (for Linux)

You can use the following function to get current user name is Linux. I have tested this on Ubuntu and CentOS flavors but it should also work with other flavors without any issues.

Basically it uses whoami program to query the current user name and read the outputs of the process:

QString MyClass::getCurrentUserNameLinux()
{
	QProcess process(this);
	process.setProgram("whoami");
	process.start();
	while (process.state() != QProcess::NotRunning)
		qApp->processEvents();
	return process.readAll();
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.