Qt - 将int转换为文件大小格式(KB,MB或GB)的最简单方法?

时间:2015-06-20 19:29:32

标签: qt

将文件大小int转换为文件大小格式的字符串的最简单方法是:

2048 to 2 KB
4086 KB to 4 MB

而不是在Qt5中手动计算?

3 个答案:

答案 0 :(得分:15)

QFileInfo没有特定于此的方法,但是here你可以找到一个简单的实现子类化QFileInfo并实现这个新方法

QString QFileInfoHumanSize::size_human()
{
    float num = this->size();
    QStringList list;
    list << "KB" << "MB" << "GB" << "TB";

    QStringListIterator i(list);
    QString unit("bytes");

    while(num >= 1024.0 && i.hasNext())
     {
        unit = i.next();
        num /= 1024.0;
    }
    return QString().setNum(num,'f',2)+" "+unit;
}

答案 1 :(得分:15)

Qt 5.10引入了ready solution

elem.style[styleProperty] = styleValue; <- i.e: styleValue = 'fixed'
           ^
           |_ Variable styleProperty, i.e: styleProperty = 'position'

答案 2 :(得分:0)

如果与int而不是浮点一起使用,@ danielfranca提供的代码会导致稍微不正确的结果。这是一个更准确的版本,用于将结果报告为整数:

QString MyClass::convertFileSizeToHumanReadable(const qlonglong & bytes)
{
    QString number;

    if(bytes < 0x400) //If less than 1 KB, report in B
    {
        number = QLocale::system().toString(bytes);
        number.append(" B");
        return number;
    }
    else
    {
        if(bytes >= 0x400 && bytes < 0x100000) //If less than 1 MB, report in KB, unless rounded result is 1024 KB, then report in MB
        {
            qlonglong result = (bytes + (0x400 / 2)) / 0x400;

            if(result < 0x400)
            {
                number = QLocale::system().toString(result);
                number.append(" KB");
                return number;
            }
            else
            {
                qlonglong result = (bytes + (0x100000 / 2)) / 0x100000;
                number = QLocale::system().toString(result);
                number.append(" MB");
                return number;
            }
        }
        else
        {
            if(bytes >= 0x100000 && bytes < 0x40000000) //If less than 1 GB, report in MB, unless rounded result is 1024 MB, then report in GB
            {
                qlonglong result = (bytes + (0x100000 / 2)) / 0x100000;

                if(result < 0x100000)
                {
                    number = QLocale::system().toString(result);
                    number.append(" MB");
                    return number;
                }
                else
                {
                    qlonglong result = (bytes + (0x40000000 / 2)) / 0x40000000;
                    number = QLocale::system().toString(result);
                    number.append(" GB");
                    return number;
                }
            }
            else
            {
                if(bytes >= 0x40000000 && bytes < 0x10000000000) //If less than 1 TB, report in GB, unless rounded result is 1024 GB, then report in TB
                {
                    qlonglong result = (bytes + (0x40000000 / 2)) / 0x40000000;

                    if(result < 0x40000000)
                    {
                        number = QLocale::system().toString(result);
                        number.append(" GB");
                        return number;
                    }
                    else
                    {
                        qlonglong result = (bytes + (0x10000000000 / 2)) / 0x10000000000;
                        number = QLocale::system().toString(result);
                        number.append(" TB");
                        return number;
                    }
                }
                else
                {
                    qlonglong result = (bytes + (0x10000000000 / 2)) / 0x10000000000; //If more than 1 TB, report in TB
                    number = QLocale::system().toString(result);
                    number.append(" TB");
                    return number;
                }
            }
        }
    }
}