当前位置: 首页 > news >正文

【Qt】获取当前系统用户名:9种获取方式

目的

有时,在项目开发中,需要显示或者用到当前系统用户名信息。以下是几种获取系统用户名解决方案:

解决方案

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. 使用qgetenvgetenv

#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();
}
http://www.lryc.cn/news/246537.html

相关文章:

  • ECMAScript2023你学习了吗?
  • 【从删库到跑路 | MySQL总结篇】数据库基础(增删改查的基本操作)
  • 【JMeter】配置元件
  • 数据采集静态存储SRAM芯片EMI7064
  • 网络运维与网络安全 学习笔记2023.11.27
  • ansible学习
  • 使用Kibana让es集群形象起来
  • 机器学习调参指南:提升模型性能的关键步骤
  • 图书管理系统源码,图书管理系统开发,图书借阅系统源码四TuShuManager应用程序MVC视图View
  • Visual Studio2010保姆式安装教程(VS2010 旗舰版),以及如何运行第一个C语言程序,超详细
  • 第四节HarmonyOS 熟知开发工具DevEco Studio
  • 安防视频监控/视频融合/云存储EasyCVR页面数据显示不全该如何解决?
  • vatee万腾的数字化奇点:Vatee科技的前沿创新之路
  • C#,《小白学程序》第六课:队列(Queue)其二,队列的应用,编写《实时叫号系统》
  • 打造数字人偶像的意义与影响
  • Spring加载Bean的多种方式
  • minio分布式存储系统
  • Kafka 如何保证消息消费的全局顺序性
  • boa+cgi上传文件超过1M报错问题
  • 抖去推--短视频账号矩阵系统saas工具源码技术开发(源头)
  • 【服务器能干什么】搭建一个短网址平台,可以查看数据详情!
  • MySQL备份与恢复(重点)
  • 机器学习中的特征选择:方法和 Python 示例
  • 有哪些不错的golang开源项目?
  • 解决ssh使用public key远程登录服务器拒绝问题
  • js数组中,相同id的item数据合并
  • LeetCode(33)最小覆盖子串【滑动窗口】【困难】
  • 设计模式 创建者模式
  • 排序算法--插入排序
  • 【操作宝典】SQL巨擘:掌握SQL Server Management的终极秘籍!