0

I'm trying to parse a QString character by character with a while loop, but I can't figure out how to parse an individual character to char type. Here's my code, I know it's not optimal:

QString temp = (QString)t[0];
int i = 1;

while (t[i] != " ");
{
    temp.append(t[i]);
    i += 1;
}

I've seen the casting with toLocal8bit function, but whatever I try I just cannot adapt it to my code.

Qt Creator shows this error:

error: conversion from 'const char [2]' to 'QChar' is ambiguous

in line with the while function call

2
  • Given that t[x] seems to refer to some sort of character, the (QString)t[0] cast seems very fishy. Sep 20, 2013 at 11:57
  • Qt Creator accepted that casting without problem. Initially it gave me an error without it Sep 20, 2013 at 14:27

2 Answers 2

2

You can use C++ 11 range based for loop

for (auto chr : text)
{
    if (!chr.isDigit()) // for exmpl.
    return false; 
}
1
  • 4
    Please add some explanation how this code solves the issue
    – Anna
    Nov 1, 2019 at 1:28
1

Why don't you try that :

QString test = "test";


for(int i = 0; i< test.length(); i++)
{
    if (test.at(i) != " ")
        test.at(i).toLatin1();
}
6
  • Using at function still gives me the same error: "error: conversion from 'const char [2]' to 'QChar' is ambiguous" Sep 20, 2013 at 14:26
  • This code run like a charm for me, did you adapted it? Feel free to paste your actual code (or a part of it) instead of a snippet Sep 20, 2013 at 14:33
  • Yes I adapted it by changing t[i] != " " to t.at(i) != " ", bit it failed yet again at debugging Sep 20, 2013 at 14:35
  • it seems that your cast QString temp = (QString)t[0]; doesn't work well, do you really need it? Sep 20, 2013 at 14:39
  • I modified my code to only declare temp but not initialise it, and I've also commented out temp.append(t[i]), but the error persists Sep 20, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.