【问题标题】:QString replace elementQString替换元素
【发布时间】:2019-01-23 06:24:52
【问题描述】:

我尝试替换QString img=":/images/f0000.png";

如果 pVaule 是 51 img 应该是":/images/f0001.png"

如果 pVaule 是 71 img 应该是":/images/f0021.png"

但我的结果是 pVaule 是 51 img

":/images/f000\u0001.png"

pVaule 是 71 img

":/images/f00\u0002\u0001.png"

如何解决?

【问题讨论】:

    标签: c++ qt5 qstring


    【解决方案1】:

    你必须使用数字的 ASCII 值...

    if(pValue>=50 && pValue<=89)
    {
        QString img=":/images/f0000.png";
        if(pValue>=50 && pValue<=59)
        {
            img.data()[12]='0';
            img.data()[13]='0'+char(pValue-50);
        }
        else if(pValue>=60 && pValue<=89)
        {
            img.data()[12]='0'+char(pValue-50)/10;
            img.data()[13]='0'+char(pValue-50)%10;
        }
    }
    

    另外,更好的解决方案是:

    img.replace(10, 4, QString(4 - QString::number(pValue - 50).length()), '0') + QString::number(pValue - 50));
    

    【讨论】:

    • 我对一行需要时间来弄清楚有点困惑,但谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多