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
Qt 6.11 is out! See what's new in the release blog

Converting qstring into qlist

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 18.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 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 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 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 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 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
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Thank you very much for your help.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on 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

                • Login

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