Search code examples
qtintegertelnetqstring

Getting a number from QString then converting it to integer


Iam developing a QT software which is using telnet commands to get some information from the adsl modem link. i got all those information to a QString

 QString datarate = ui->output->toPlainText();

i want to select only a number after downstream rate

Downstream rate = 10239 Kbps

and convert it to integer to compare it with other numbers ... i don't want to take the first one which is = 20892 kbps

Status: Showtime
Max:    Upstream rate = 1193 Kbps, Downstream rate = 20892 Kbps

Bearer: 0, Upstream rate = 1021 Kbps, Downstream rate = 10239 Kbps

any advices?? note : the number will be random so adsl status are much different


Solution

  • Maybe something like this will work:

    QString datarate = ui->output->toPlainText();
    int number = datarate.split("Downstream rate = ")[2].split(" ")[0].toInt();
    

    This is taking the following steps:

    1. Breaking up the string into pieces separated by "Downstream rate = "
    2. Taking the third string in that list (Should contain "10239 Kbps")
    3. Splitting that by space characters and taking the first token. (Should contain "10239")
    4. Finally converting the string to an int.