有时,在项目开发中,需要显示或者用到当前系统用户名信息。以下是几种获取系统用户名解决方案:
QDir::home()
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDir dir;
QString userName = dir.home().dirName();
qDebug().noquote() << userName;
return a.exec();
}
QProcessEnvironment
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
// qDebug().noquote() << env.value("USER"); // Linux
qDebug().noquote() << env.value("USERNAME"); // Windows
return a.exec();
}
QProcess
#include
#include
#include
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();
}
在.pro中配置
QT += network
#include
#include
#include
int main(int argc ,char* argv[])
{
QApplication a(argc, argv);
QString machineName = QHostInfo::localHostName();
qDebug().noquote() << machineName;
return a.exec();
}
qgetenv
或getenv
#include
#include
int main(int argc ,char* argv[])
{
QApplication a(argc, argv);
// 可以使用qgetenv代替getenv
// for MAc or Linux
qDebug().noquote() << qgetenv("USER");
// for windows
qDebug().noquote() << getenv("USERNAME");
return a.exec();
}
QStandardPaths::standardLocations
#include
#include
#include
int main()
{
QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
qDebug() << homePath.first().split('/').last();
return 0;
}
GetUserName
#include
#include
#include
#define UNLEN 128
int 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;
}
#include
#include
#include
#include
#include
#define MAX_USERNAME 128
int main(int argc, char **argv)
{
// Strictly pseudo code!
#ifdef Q_OS_WIN
wchar_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_UNIX
QCoreApplication 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
}
#include
#include
#include
QString getSystemUsername()
{
QString username;
#ifdef Q_OS_WIN
QProcess 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();
#endif
return username;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString username = getSystemUsername();
qDebug() << "当前系统用户名:" << username;
return a.exec();
}