Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Build multiline QString from single line QString
Forum Update on Tuesday, May 27th 2025

Build multiline QString from single line QString

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marklasta
    wrote on 25 Jan 2014, 12:11 last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 25 Jan 2014, 12:42 last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gorbatschow
        wrote on 25 Jan 2014, 12:43 last edited by
        #3

        You can try the newline character '\n'

        1 Reply Last reply
        0
        • M Offline
          M Offline
          marklasta
          wrote on 25 Jan 2014, 12:56 last edited by
          #4

          I don't need to split the strig after "0.6". I need to split the string every n positions.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JKSH
            Moderators
            wrote on 25 Jan 2014, 13:08 last edited by
            #5

            Then insert a newline character every n positions.

            Do you have any experience with string manipulation (in any programming language)?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on 25 Jan 2014, 13:10 last edited by
              #6

              A loop "QString mid()":http://qt-project.org/doc/qt-5/qstring.html#mid seems to be a good solution then.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                marklasta
                wrote on 25 Jan 2014, 20:29 last edited by
                #7

                I'm trying a lot of solutions but it won't work !

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 25 Jan 2014, 21:43 last edited by
                  #8

                  [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.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Jan 2014, 21:46 last edited by
                    #9

                    Hi,

                    Then please show us your code, it will be faster to help you

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      marklasta
                      wrote on 27 Jan 2014, 07:46 last edited by
                      #10

                      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.1

                      The output should appear in a matrix-like style in the QTextBrowser.

                      Thank you in advance.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 27 Jan 2014, 08:41 last edited by
                        #11

                        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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          marklasta
                          wrote on 27 Jan 2014, 10:36 last edited by
                          #12

                          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);@

                          1 Reply Last reply
                          0

                          1/12

                          25 Jan 2014, 12:11

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved