目的
有时,在项目开发中,需要显示或者用到当前系统用户名信息。以下是几种获取系统用户名解决方案:
解决方案
1. 使用QDir::home()
#include <QApplication>
#include <QDir>
#include <QDebug>int main(int argc, char *argv[])
{QApplication a(argc, argv);QDir dir;QString userName = dir.home().dirName();qDebug().noquote() << userName;return a.exec();
}
2. 使用QProcessEnvironment
#include <QApplication>
#include <QDebug>
#include <QProcessEnvironment>int main(int argc, char *argv[])
{QApplication a(argc, argv);QProcessEnvironment env = QProcessEnvironment::systemEnvironment();// qDebug().noquote() << env.value("USER"); // LinuxqDebug().noquote() << env.value("USERNAME"); // Windowsreturn a.exec();
}
3. 使用QProcess
#include <QApplication>
#include <QDebug>
#include <QProcess>int main(int argc, char *argv[])
{QApplication a(argc, argv);QStringList env = QProcess::systemEnvironment();// foreach(QString envs, env){// qDebug() << envs; // 输出所有系统配置int index = env.indexOf(QRegExp("USERNAME.*")); // 正则表达式,模糊匹配QString userName = env.at(index); // 字符串: "USERNAME=admin"// userName.remove(0,9); // 删去从首个字符开始的9个字符即"USERNAME="userName.remove("USERNAME=");qDebug().noquote() << userName;// }return a.exec();
}
4. 使用QHostInfo::localHostName(),建议这个
在.pro中配置
QT += network
#include <QApplication>
#include <QDebug>
#include <QHostInfo>int main(int argc ,char* argv[])
{QApplication a(argc, argv);QString machineName = QHostInfo::localHostName();qDebug().noquote() << machineName;return a.exec();
}
5. 使用qgetenv
或getenv
#include <QApplication>
#include <QDebug>int main(int argc ,char* argv[])
{QApplication a(argc, argv);// 可以使用qgetenv代替getenv// for MAc or LinuxqDebug().noquote() << qgetenv("USER");// for windowsqDebug().noquote() << getenv("USERNAME");return a.exec();
}
6. 使用QStandardPaths::standardLocations
#include <QStandardPaths>
#include <QStringList>
#include <QDebug>int main()
{QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);qDebug() << homePath.first().split('/').last();return 0;
}
7. 使用GetUserName
#include <QDebug>
#include <Windows.h>
#include <iostream>#define UNLEN 128int main() {TCHAR username[UNLEN + 1]; // 定义一个足够大的缓冲区来存储用户名DWORD usernameLength = UNLEN + 1; // 指定缓冲区的大小if (GetUserName(username, &usernameLength)) {std::wcout << L"当前系统用户名: " << username << std::endl; // 输出用户名} else {std::cout << "获取用户名失败,错误代码: " << GetLastError() << std::endl; // 输出错误信息}return 0;
}
8. 获取Linux系统用户名
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <windows.h>
#include <iostream>#define MAX_USERNAME 128int main(int argc, char **argv)
{
// Strictly pseudo code!
#ifdef Q_OS_WINwchar_t acUserName[MAX_USERNAME];DWORD nUserName = sizeof(acUserName);if (GetUserName(acUserName, &nUserName))std::cout << acUserName << std::endl;else {qDebug().noquote() << "aaa";}// qDebug << acUserName;return 0;
#elif Q_OS_UNIXQCoreApplication coreApplication(argc, argv);QProcess process;QObject::connect(&process, &QProcess::finished, [&coreApplication, &process](int exitCode, QProcess::ExitStatus exitStatus) {qDebug() << process.readAllStandardOutput();coreApplication.quit();});process.start("whoami");return coreApplication.exec();
#endif
}
9. 使用命令获取Windows或Linux系统用户名
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>QString getSystemUsername()
{QString username;#ifdef Q_OS_WINQProcess process;process.start("cmd.exe", QStringList() << "/c" << "echo %username%");process.waitForFinished();username = QString::fromLocal8Bit(process.readAllStandardOutput().trimmed());#elif defined(Q_OS_LINUX) || defined(Q_OS_MACOS)QProcess process;process.start("whoami");process.waitForFinished();username = QString::fromLocal8Bit(process.readAllStandardOutput()).trimmed();#endifreturn username;
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QString username = getSystemUsername();qDebug() << "当前系统用户名:" << username;return a.exec();
}