qt获取本机用户名

//获取用户名  
QString getUserName()  
{  
#if 1  
    QStringList envVariables;  
    envVariables << "USERNAME.*" << "USER.*" << "USERDOMAIN.*"  
                 << "HOSTNAME.*" << "DOMAINNAME.*";  
    QStringList environment = QProcess::systemEnvironment();  
    foreach (QString string, envVariables) {  
        int index = environment.indexOf(QRegExp(string));  
        if (index != -1) {  
            QStringList stringList = environment.at(index).split('=');  
            if (stringList.size() == 2) {  
                return stringList.at(1);  
                break;  
            }  
        }  
    }  
    return "unknown";  
#else  
    QString userName = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);  
    userName = userName.section("/", -1, -1);  
    return userName;  
#endif  
}  

 

posted @ 2017-12-11 10:09  风来风往风伤(TiAmo)  阅读(2501)  评论(0编辑  收藏  举报