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

技术调研:QStandardPaths

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
    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");
    }

 

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

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄