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

Posted on Posted in Qt Tutorials, Tutorials

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);
while(process.state() != QProcess::NotRunning)
return process.readAll();

Leave a Reply

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