Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to list foreach from array

    General and Desktop
    6
    11
    39939
    Loading More Posts
    • 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.
    • B
      betmens last edited by

      Hi.
      At this time I write second code:
      @QStrinList list, list2;
      ...
      list2.clear();
      list2 << "data1" << "data2" ;
      foreach (QString v, list2) list.removeOne(v);@
      Is here a way to write simply without temporary list2 declaration?
      @QStrinList list;
      ...
      foreach (QString v, {"data1", "data2"}) list.removeOne(v);@
      or
      @QStrinList list;
      ...
      QString array[] = {"data1", "data2"};
      foreach (QString v, array) list.removeOne(v);@
      In php, javascript and pascal it is possible.

      1 Reply Last reply Reply Quote 0
      • J
        Jake007 last edited by

        What exactly are you trying to a achieve?
        As far as I know, foreach in Qt is completely the same as in php ( functionality, syntax is slightly different).


        Code is poetry

        1 Reply Last reply Reply Quote 0
        • F
          francomartins last edited by

          Try it :

          @ QStringList list;

          list <<"One" <<"Two" <<"..." <<".....";

          foreach (QString v,list){
          v.replace("One","Ten");
          qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
          }@

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            If I get the question correctly, you want to remove a series of elements from a list efficiently. Right?

            I'm not sure what syntactic sugar you want though. If you want to delete multiple items at once, you'll have to put those items in a list or array somehow anyway. Your second alternative is just as long as your original code, barring the needless list2.clear() call.

            1 Reply Last reply Reply Quote 0
            • K
              Kxyu last edited by

              Try this:
              @
              QStrignList list;
              ...
              foreach (QString v, QStringList({"data1", "data2"})) list.removeOne(v);
              @

              This a 4.8 feature, C++11 only. QStringList::QStringList ( std::initializer_list<QString> args ) constructor is used.

              1 Reply Last reply Reply Quote 0
              • B
                betmens last edited by

                [quote author="Andre" date="1332007005"]If I get the question correctly, you want to remove a series of elements from a list efficiently. Right?

                I'm not sure what syntactic sugar you want though. If you want to delete multiple items at once, you'll have to put those items in a list or array somehow anyway. Your second alternative is just as long as your original code, barring the needless list2.clear() call. [/quote]

                Remove a series of elements from the list is only one example of project.
                Essentially I want to get array from database and then reflect output results to a program. Mainly using foreach function.
                For example I have tables:
                @0|data1
                1|data2
                2|data3
                output:
                array[] = ({"data1", "ident2", "ident2"});
                foreach (Qstring v, array) {do something with v};

                or
                1|"name1"|"data1"
                2|"name2"|"data2"
                5|"name3"|"data3"
                output:
                array[1] or array["name1"] = "data1"
                array[2] or array["name2"] = "data2"
                array[5] or array["name3"] = "data3"

                or
                1|"name1"|"data11"|"data12"
                2|"name2"|"data21"|"data22"
                5|"name3"|"data31"|"data32"
                output:
                array[1] or array["name1"] = ({"data11", "data12"});
                array[2] or array["name2"] = ({"data21", "data22"});
                array[5] or array["name3"] = ({"data31", "data32"});
                foreach (Qstring v, array["name2"]) {do something with v};

                or lang table:
                id|name|en|lv
                0|"name1"|"en text 1"|"lv text 1"
                1|"name2"|"en text 2"|"lv text 2"
                2|"name3"|"en text 3"|"lv text 3"
                output:
                array["en"] = {"en text 1", "en text 2", "en text 3"});
                array["lv"] = {"lv text 1", "lv text 2", "lv text 3"});
                and
                array["en"][0] or array["en"]["name1"] = "en text 1"
                array["en"][1] or array["en"]["name2"] = "en text 2"
                array["en"][2] or array["en"]["name3"] = "en text 3"
                array["lv"][0] or array["lv"]["name1"] = "lv text 1"
                array["lv"][1] or array["lv"]["name2"] = "lv text 2"
                array["lv"][2] or array["lv"]["name3"] = "lv text 3"
                QString lang = "en";
                foreach (Qstring v, array[lang]) {do something with v};
                array[lang][1] or array[lang]["name2"] = "en text 2"
                @
                Every time make from array QList I think is not good solution.
                [quote author="Kxyu" date="1332059432"]Try this:
                @
                QStrignList list;
                ...
                foreach (QString v, QStringList({"data1", "data2"})) list.removeOne(v);
                @
                This a 4.8 feature, C++11 only. QStringList::QStringList ( std::initializer_list<QString> args ) constructor is used.[/quote]

                It would be perfect, but not working on latest "Offline installer - 1.3 GB/ Based on Qt 4.7.4(32bit)" on win 7. And where I can see C++ version? :)
                May be I need to install Qt Creator and libraries separately?

                1 Reply Last reply Reply Quote 0
                • G
                  goetz last edited by

                  [quote author="francomartins" date="1331987833"]Try it :

                  @QStringList list;

                  list <<"One" <<"Two" <<"..." <<".....";

                  foreach (QString v,list) {
                  v.repleace("One","Ten");
                  qDebug()<<v ; //returns "Ten" , "Two" , "..." , "....."
                  @
                  [/quote]

                  Sorry, but thats plain wrong for multiple reasons.

                  foreach creates a copy of the container you put into the second argument. So you are modifying the copy and not the original container. Second, for each QString in the container you create a copy with the first argument v. So you are not even modifying the copy of the container, but only copies of each item in it.

                  If you had actually run you snippet you would have seen that. BTW: It even doesn't compile, which proofs to me that you didn't run it. A slightly modified and running version is:

                  @
                  QStringList list;
                  list <<"One" <<"Two" <<"..." <<".....";

                  qDebug() << "List before:" << list;

                  foreach (QString x, list) {
                  x.replace("One","Ten");
                  }

                  qDebug() << "List after:" << list;
                  @

                  and the output is - surprise:

                  @
                  List before: ("One", "Two", "...", ".....")
                  List after: ("One", "Two", "...", ".....")
                  @

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply Reply Quote 0
                  • F
                    francomartins last edited by

                    Hello Mr Immortal, the mistake was not finishing the foreach. try it now

                    @ QStringList list,list2;

                    list <<"One" <<"Two" <<"..." <<".....";

                    foreach (QString v,list){
                    v.replace("One","Ten");
                    qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
                    };@
                    the qDebug () is in QString, QStringList, and not the more can be done also in the QStringList;
                    try it :
                    @
                    foreach (QString v,list){
                    v.replace("One","Ten");
                    list2 << v;
                    }
                    qDebug()<<list2.join(","); //returns "Ten" , "Two" , "..." , "....." @

                    1 Reply Last reply Reply Quote 0
                    • B
                      betmens last edited by

                      How I tried "replace" works correctly. Only You must be careful. QStringList list<<"twenty one"<<"twenty two" with replace("one","ten") give result: "twenty ten","twenty two"

                      To francomartins. Maybe write just simple:
                      @QStringList list <<"One"<<"Two"<<"...";
                      list.replaceInStrings("One","Ten");
                      qDebug()<<list ; //returns ("Ten", "Two", "...")@
                      But is here somebody who can give an answer to a topic main question?

                      1 Reply Last reply Reply Quote 0
                      • B
                        betmens last edited by

                        @QStrinList list, list2;
                        ...
                        list2.clear();
                        list2 << "data1" << "data2" ;
                        foreach (QString v, list2) list.removeOne(v);@
                        I replace with:
                        @QStrinList list;
                        ...
                        foreach (v, QStringList()<<"data1"<<"data2") list.removeOne(v);@

                        1 Reply Last reply Reply Quote 0
                        • A
                          andre last edited by

                          Sure, it's less lines, but I doubt it is more readable. I find the readability issue important.

                          An interesting read on designing for readable code, and a good summary of why the Qt API looks the way it does, can be found in "The Little Manual of API Design":http://www4.in.tum.de/~blanchet/api-design.pdf . I think it's a good read.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post