Build multiline QString from single line QString
-
Suppose you have a QString just like this
@QString inputString = "0.4 0.2 0.1 0.6 0.3 0.7 0.1 0.8";@
what I need to do is to create another QString, say "outputString", as a multiline version of the first one.
outputString should looks like this
0.4 0.2 0.1 0.6
0.3 0.7 0.1 0.8 -
Hi,
You want to replace the space after "0.6" with a newline character ('\n'). See the "QString documentation":http://qt-project.org/doc/qt-5/qstring.html#operator-5b-5d for more details.
-
You can try the newline character '\n'
-
I don't need to split the strig after "0.6". I need to split the string every n positions.
-
Then insert a newline character every n positions.
Do you have any experience with string manipulation (in any programming language)?
-
A loop "QString mid()":http://qt-project.org/doc/qt-5/qstring.html#mid seems to be a good solution then.
-
I'm trying a lot of solutions but it won't work !
-
[quote author="marklasta" date="1390681760"]I'm trying a lot of solutions but it won't work ![/quote]"It won't work" is a very vague statement. It doesn't show us what the problem is, so we can't help you.
Instead, please show us the code to the solutions that you tried. That will show us what's wrong, and then we'll know how to help you.
-
Hi,
Then please show us your code, it will be faster to help you
-
Yep, it's true... I'm sorry for the vagueness.
Please find here my code.
@ QString spacer = " ";
int startIndex = 0; int endIndex = 30; int spacerLength = spacer.length(); for (int i = 0; i < stringMatrix.length(); i++) { outputString.push_back(stringMatrix.section("", startIndex, endIndex)); startIndex = endIndex + 1; endIndex = endIndex + spacerLength; } ui->outputBrowser->append(outputHeaderString + spacer + outputString);@
Now, @outputString@ is equal to "0.3 0.3 0.1 0.1 0.4 0.3 0.1 0.1 0.4 0.3 0.1 0.1 0.2 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.4 0.3 0.1 0.1 0.2 0.2 0.1 0.1 0.4 0.3 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1"
My main problem is about formatting the string this way
0.3 0.3 0.1 0.1
0.4 0.3 0.1 0.1
0.4 0.3 0.1 0.1
0.2 0.2 0.1 0.1
[...]
0.1 0.1 0.1 0.1The output should appear in a matrix-like style in the QTextBrowser.
Thank you in advance.
-
You don't put line breaks anywhere so the output is correct in that aspect.
You need to either add a "\n" to each line or append each line directly in your outputBrowser
-
Just got a solution.
@
QString spacer = " ";QString newLine = "\n";
int startIndex = 0;
int endIndex = 30;
int increment = (endIndex + spacer.length());
for (int i = 0; i < ROW_NUM; i++)
{
outputString.push_back(stringMatrix.section("", startIndex, endIndex) + newLine);startIndex = (endIndex + 1); endIndex += increment;
}
ui->outputBrowser->append(spacer + outputString);@