how to convert QString into int in qt creator

c++, int, qt, string

Solution

You need to get rid of the `bitrate:` prefix and `kb/s` suffix. The best way is to use `QRegExp` or `QRegularExpression` to extract the digit part of the string and then call `toInt()`.

Here's example:

QString str = "bitrate: 3543 kb/s";
int value;

QRegExp re("bitrate:\\s*(\\d+)\\s*.*");
if (re.indexIn(str) != -1) {
    value = re.cap(1).toInt();
} else {
    qDebug() << "String not matched.";
    return;
}
qDebug() << value;

Problem

I have seen one answer regarding to this same question but when I tried it with my problem it get confused. here is my string, ``` bit_rate = "bitrate: 2334 kb/s" ``` I need to get the `2334` from this string and assign it into a integer variable.. how is this possible in qt creator. I tried `toInt()`, but it always gives `0` as the answer.

Original source

Related problems