QVarient outputting bogus data
-
Apart from the strange conversion you're doing please show the contents of values[] and the value of z in a minimal, compilable example.
-
Sorry should've given those already.
A basic example of what i'm doing would be:QString data(8, *) //Set temp data into the string int values[7] = {1,2,3,4}; for(z=0;z<8;z++) { if(z % 2 != 0) { //I probably don't need this bit if toChar() works properly, // I was just trying to locate the error. data[z] = ','; } else { data[z] = QVariant(values[z]).toChar(); } } qDebug() << "output: " << data; //And this outputs 1,2,3,4 but as a string
I know its odd, but I need it as a string sadly. There may be a better conversion method that I've not found
-
You're accessing values{z] out-of-bounds.
btw: Take a look at QString::number().
-
You're accessing values{z] out-of-bounds.
btw: Take a look at QString::number().
@Christian-Ehrlicher
If you mean out of bounds because my loop is bigger than the array; that's a type by me they're actually both set to the same integer variable.With QString::number() I get somekind of overload error:
for(int z=0; z<longestString; z++) { //Set data to actaul values tempChar = QString::number(values[z]); if(z % 2 != 0) { data[z] = ','; } else { data[z] = tempChar; //error = no viable overload '=' } }
I did actually try this method already but couldn't get around this error.
-
@Christian-Ehrlicher
If you mean out of bounds because my loop is bigger than the array; that's a type by me they're actually both set to the same integer variable.With QString::number() I get somekind of overload error:
for(int z=0; z<longestString; z++) { //Set data to actaul values tempChar = QString::number(values[z]); if(z % 2 != 0) { data[z] = ','; } else { data[z] = tempChar; //error = no viable overload '=' } }
I did actually try this method already but couldn't get around this error.
@SOrton said in QVarient outputting bogus data:
data[z] = tempChar; //error = no viable overload '='
Because data[z] refers to a QChar and you try to assign a QString.
Use QString data; ... data += QString::number(whatever) instead array access.that's a type by me they're actually both set to the same integer variable.
But even then your loop is not correct since your output would then contain
1,3,5,7,
Fix your code and post it again (completely)
-
@SOrton said in QVarient outputting bogus data:
data[z] = tempChar; //error = no viable overload '='
Because data[z] refers to a QChar and you try to assign a QString.
Use QString data; ... data += QString::number(whatever) instead array access.that's a type by me they're actually both set to the same integer variable.
But even then your loop is not correct since your output would then contain
1,3,5,7,
Fix your code and post it again (completely)
@Christian-Ehrlicher [link text]
I tried to use string, because char was causing me to get random outputs. Ill go back to my original attempt and show you my outputs.
Also the last , makes sense what you say but it doesn't seem to be happening, not sure why.code:
//I cant give the code where these variables are set as the program is too big but //but ill set them to exact values they're in my program at this points in time. int longestString = 7; int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers. qDebug() << "longest string" << longestString; QString data(longestString, '*'); //create data string with bogus data qDebug() << "Initial data" << data; char tempChar; for(int z=0; z<longestString; z++) { //Set data to actaul values tempChar = values[z]; //using char(values[z]) yields same result if(z % 2 != 0) { data[z] = ','; } else { data[z] = tempChar; } } qDebug() << "new data " << data;
The final debug gives an output of: "\u0001,\b,\u0000,\u0000"
So the , are placed correctly (without one at the end) and the chars I'm not sure what's happening
I believe these must be unicode. How do i stop it going to unicode/convert it back to true char?
-
@Christian-Ehrlicher [link text]
I tried to use string, because char was causing me to get random outputs. Ill go back to my original attempt and show you my outputs.
Also the last , makes sense what you say but it doesn't seem to be happening, not sure why.code:
//I cant give the code where these variables are set as the program is too big but //but ill set them to exact values they're in my program at this points in time. int longestString = 7; int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers. qDebug() << "longest string" << longestString; QString data(longestString, '*'); //create data string with bogus data qDebug() << "Initial data" << data; char tempChar; for(int z=0; z<longestString; z++) { //Set data to actaul values tempChar = values[z]; //using char(values[z]) yields same result if(z % 2 != 0) { data[z] = ','; } else { data[z] = tempChar; } } qDebug() << "new data " << data;
The final debug gives an output of: "\u0001,\b,\u0000,\u0000"
So the , are placed correctly (without one at the end) and the chars I'm not sure what's happening
I believe these must be unicode. How do i stop it going to unicode/convert it back to true char?
-
@mpergand I have tried simply +48 and it didnt seem to work, yielding 1,8,0,0 from the above output
in this case 1,2,8,11 becomes 1,8,0,0 (with +48) I can understand 11 not working but not sure about the other two.
I never claimed to be a top programmer, and i am learning by doing. I am thankful for all the help you guys give me but I don't think saying that is necessary ;/
-
@mpergand I have tried simply +48 and it didnt seem to work, yielding 1,8,0,0 from the above output
in this case 1,2,8,11 becomes 1,8,0,0 (with +48) I can understand 11 not working but not sure about the other two.
I never claimed to be a top programmer, and i am learning by doing. I am thankful for all the help you guys give me but I don't think saying that is necessary ;/
-
@SOrton said in QVarient outputting bogus data:
int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers.
this is still wrong - there are only 4 numbers so it should be values[4]...
And here the simple version of your code:
values[4] = {1, 2, 3, 4} QString output; for (int val : values) output += QString::number(val) + ',';
--> output = "1,2,3,4"