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

目的

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

解决方案

1. 使用QDir::home()

cpp 复制代码
#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

cpp 复制代码
#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"); // Linux
    qDebug().noquote() << env.value("USERNAME"); // Windows
    return a.exec();
}

3. 使用QProcess

cpp 复制代码
#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中配置

cpp 复制代码
QT       += network
cpp 复制代码
#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

cpp 复制代码
#include <QApplication>
#include <QDebug>

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();
}

6. 使用QStandardPaths::standardLocations

cpp 复制代码
#include <QStandardPaths>
#include <QStringList>
#include <QDebug>

int main()
{
    QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
    qDebug() << homePath.first().split('/').last();
    return 0;
}

7. 使用GetUserName

cpp 复制代码
#include <QDebug>
#include <Windows.h>
#include <iostream>

#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;
}

8. 获取Linux系统用户名

cpp 复制代码
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <windows.h>
#include <iostream>

#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
}

9. 使用命令获取Windows或Linux系统用户名

cpp 复制代码
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

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();
}
相关推荐
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054965 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月5 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237175 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian5 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡5 天前
简单工厂模式
开发语言·算法·c#