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. Converting qstring into qlist
Forum Updated to NodeBB v4.3 + New Features

Converting qstring into qlist

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 16.5k 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.
  • D Offline
    D Offline
    depecheSoul
    wrote on 15 Jan 2012, 10:06 last edited by
    #1

    Hello.

    Is there any way thatI can conver a qstring into a qlist.

    Here is a example:
    string: "1 4 2 6 4"
    list: 1 4 2 6 4

    I am trying to do this because I want to make a simple program GUI that will recive a array wth a qlineedit, then it will do the sorting, and the it will display a sorted list.

    Here is a example:
    input: 1 5 2 4 3
    output: 1 2 3 4 5

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sibyx
      wrote on 15 Jan 2012, 10:15 last edited by
      #2

      Try "QString::split":http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split function. It convert to QStringList but I think it should be enough

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rahul Das
        wrote on 15 Jan 2012, 10:18 last edited by
        #3

        You just need to "split":http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split the QString. The result is a list of strings.

        EDIT : oops, post is delayed. Anyway, to sort, you need QList of Int and use "qSort":http://developer.qt.nokia.com/doc/qt-4.8/qtalgorithms.html#qSort


        Declaration of (Platform) independence.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sibyx
          wrote on 15 Jan 2012, 10:31 last edited by
          #4

          @QList<int> list;
          QString input;
          QStringList slist;
          slist = input.split(" ");
          for (int x = 0; x <= slist.count()-1; x++) {
          list.append(slist.at(x).toInt());
          }@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 15 Jan 2012, 11:26 last edited by
            #5

            This works, but please use some error checking:

            @QList<int> list;
            QString input;
            QStringList slist;
            slist = input.split(" ");
            bool allOk(true);
            bool ok;
            for (int x = 0; x <= slist.count()-1 && allOk; x++) {
            list.append(slist.at(x).toInt(&ok));
            allOk &= ok;
            }

            if (!allOk) {
            // handle the problem
            }
            @

            1 Reply Last reply
            0
            • D Offline
              D Offline
              depecheSoul
              wrote on 15 Jan 2012, 14:54 last edited by
              #6

              Thank you very much for your help.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on 16 Jan 2012, 10:23 last edited by
                #7

                Just another example, this time taking control over more than one space between entries.

                @QString inputString = "1 3 5 6 2 4";
                QStringList listProcessing;
                QList<int> sortedList;
                bool conversionState = true;

                listProcessing = inputString.split(" ", QString::SkipEmptyParts);
                listProcessing.sort();

                for(int index = 0; index <= (listProcessing.size() - 1); ++index) {

                sortedList.append(listProcessing.at(index).toInt(&conversionState));
                
                if(!conversionState) break;
                

                }// for

                if(!conversionState) {

                // Handling the conversion error.
                
                ...
                

                }// if@

                1 Reply Last reply
                0

                1/7

                15 Jan 2012, 10:06

                • Login

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