QT获取linux下的当前用户名

故事背景:客户端启动的时候需要加载机器/home/xx/test.jpg的图片作为背景图,但是有的机器用户名叫AAA,有的机器名叫BBB,所以我需要获取当前用户的home目录

技术调研:QStandardPaths

    QString strImagePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/justtake.jpg";
    QFile file(strImagePath);
    if(file.exists())
    {
        this->setStyleSheet(QString("#firstWidget{border-image:url(%1)}").arg(strImagePath));
    }else
    {
        this->setStyleSheet("background-color:white");
    }

 

大家可以根据自己的业务需求进行扩展,评论区随时交流。

posted @ 2019-12-05 15:01  徐沛东  阅读(1846)  评论(0编辑  收藏  举报