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. How to get first element of QList
QtWS25 Last Chance

How to get first element of QList

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5qlist
17 Posts 4 Posters 4.9k Views
  • 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
    deleted286
    wrote on 23 Mar 2021, 06:18 last edited by
    #1

    Hi everyone. How can i get the first element of my list, which name is index
    I've tried to index.at() but it doesnt work.
    Index have 14, 0 ,1 ,13 respectively.
    I want to print them seperately. I will use firstly 14 and then the others by one by

                           ```
                           QList<int> index;
                           index << inner_data[1].toString().toInt();
                           for (int i = 0; i < index.size(); i++) {
                              
    
                                  qDebug() << index;
                           }
    
    J 1 Reply Last reply 23 Mar 2021, 06:33
    0
    • D deleted286
      23 Mar 2021, 06:18

      Hi everyone. How can i get the first element of my list, which name is index
      I've tried to index.at() but it doesnt work.
      Index have 14, 0 ,1 ,13 respectively.
      I want to print them seperately. I will use firstly 14 and then the others by one by

                             ```
                             QList<int> index;
                             index << inner_data[1].toString().toInt();
                             for (int i = 0; i < index.size(); i++) {
                                
      
                                    qDebug() << index;
                             }
      
      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 23 Mar 2021, 06:33 last edited by JKSH
      #2

      @suslucoder said in How to get first element of QList:

      How can i get the first element of my list

      https://doc.qt.io/qt-5/qlist.html#first

      which name is index

      This name will cause lots of confusion. The word "index" means "a position in a list". Why would you call your list "index"? Please give it a non-ambiguous name.

      I've tried to index.at() but it doesnt work.

      What do you mean by "it doesn't work"? Show your code. Show the output of your code. Show us what you expected the output to be.

      Index have 14, 0 ,1 ,13 respectively.

      I don't understand what you mean.

                             QList<int> index;
                             index << inner_data[1].toString().toInt();
      

      After this code runs, your list will contain only 1 integer.

                                    qDebug() << index;
      

      This code prints your whole list.

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

      D 1 Reply Last reply 23 Mar 2021, 06:40
      1
      • J JKSH
        23 Mar 2021, 06:33

        @suslucoder said in How to get first element of QList:

        How can i get the first element of my list

        https://doc.qt.io/qt-5/qlist.html#first

        which name is index

        This name will cause lots of confusion. The word "index" means "a position in a list". Why would you call your list "index"? Please give it a non-ambiguous name.

        I've tried to index.at() but it doesnt work.

        What do you mean by "it doesn't work"? Show your code. Show the output of your code. Show us what you expected the output to be.

        Index have 14, 0 ,1 ,13 respectively.

        I don't understand what you mean.

                               QList<int> index;
                               index << inner_data[1].toString().toInt();
        

        After this code runs, your list will contain only 1 integer.

                                      qDebug() << index;
        

        This code prints your whole list.

        D Offline
        D Offline
        deleted286
        wrote on 23 Mar 2021, 06:40 last edited by deleted286
        #3

        @JKSH

         QList<int> list;
           list << inner_data[1].toString().toInt();
             qDebug() << list;
        

        when i run this output is

        (14)
        (0)
        (1)
        (13)
        

        when i run this:

        qDebug() << list.at(0);
        

        output is

        14
        0
        1
        13
        

        I want to achieve them seperately
        I expect that when i run list.at(0) it gives me 14

        J 1 Reply Last reply 23 Mar 2021, 06:52
        0
        • D deleted286
          23 Mar 2021, 06:40

          @JKSH

           QList<int> list;
             list << inner_data[1].toString().toInt();
               qDebug() << list;
          

          when i run this output is

          (14)
          (0)
          (1)
          (13)
          

          when i run this:

          qDebug() << list.at(0);
          

          output is

          14
          0
          1
          13
          

          I want to achieve them seperately
          I expect that when i run list.at(0) it gives me 14

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 23 Mar 2021, 06:52 last edited by
          #4

          @suslucoder said in How to get first element of QList:

           QList<int> list;
             list << inner_data[1].toString().toInt();
               qDebug() << list;
          

          when i run this output is

          (14)
          (0)
          (1)
          (13)
          

          You must be running that code in a loop. Do you know that you called qDebug() 4 times?

          Every loop iteration, you put 1 item in the list and then destroy the list.

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

          D 1 Reply Last reply 23 Mar 2021, 06:57
          1
          • J JKSH
            23 Mar 2021, 06:52

            @suslucoder said in How to get first element of QList:

             QList<int> list;
               list << inner_data[1].toString().toInt();
                 qDebug() << list;
            

            when i run this output is

            (14)
            (0)
            (1)
            (13)
            

            You must be running that code in a loop. Do you know that you called qDebug() 4 times?

            Every loop iteration, you put 1 item in the list and then destroy the list.

            D Offline
            D Offline
            deleted286
            wrote on 23 Mar 2021, 06:57 last edited by
            #5

            @JKSH if you can look at my last question, the complete code is there. My mind so confused

            J 1 Reply Last reply 23 Mar 2021, 07:01
            0
            • D deleted286
              23 Mar 2021, 06:57

              @JKSH if you can look at my last question, the complete code is there. My mind so confused

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 23 Mar 2021, 07:01 last edited by JKSH
              #6

              @suslucoder said in How to get first element of QList:

              @JKSH if you can look at my last question, the complete code is there. My mind so confused

              You need to learn how to use loops.

              Add more debug messages to your code; it might help you see what's happening:

              qDebug() << "I am creating an empty list now.";
              
              QList<int> list;
              
              qDebug() << "My list contains" << list.count() << "items.";
              qDebug() << "My whole list is" << list;
              
              qDebug() << "I am putting an integer into my list now.";
              
              list << inner_data[1].toString().toInt();
              
              qDebug() << "My list contains" << list.count() << "items.";
              qDebug() << "My whole list is" << list;
              

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

              D 1 Reply Last reply 23 Mar 2021, 07:10
              1
              • J JKSH
                23 Mar 2021, 07:01

                @suslucoder said in How to get first element of QList:

                @JKSH if you can look at my last question, the complete code is there. My mind so confused

                You need to learn how to use loops.

                Add more debug messages to your code; it might help you see what's happening:

                qDebug() << "I am creating an empty list now.";
                
                QList<int> list;
                
                qDebug() << "My list contains" << list.count() << "items.";
                qDebug() << "My whole list is" << list;
                
                qDebug() << "I am putting an integer into my list now.";
                
                list << inner_data[1].toString().toInt();
                
                qDebug() << "My list contains" << list.count() << "items.";
                qDebug() << "My whole list is" << list;
                
                D Offline
                D Offline
                deleted286
                wrote on 23 Mar 2021, 07:10 last edited by
                #7

                @JKSH Thank you but i didnt understand still how can i get items seperately

                J 1 Reply Last reply 23 Mar 2021, 07:12
                0
                • D deleted286
                  23 Mar 2021, 07:10

                  @JKSH Thank you but i didnt understand still how can i get items seperately

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 23 Mar 2021, 07:12 last edited by
                  #8

                  @suslucoder Did you run the code that I posted? What did you discover?

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

                  D 1 Reply Last reply 23 Mar 2021, 07:30
                  0
                  • J JKSH
                    23 Mar 2021, 07:12

                    @suslucoder Did you run the code that I posted? What did you discover?

                    D Offline
                    D Offline
                    deleted286
                    wrote on 23 Mar 2021, 07:30 last edited by
                    #9

                    @JKSH Yes i run it. I realize that, it add the elements into list by one by.
                    I move to the definition of list to my header.

                    run this

                                         list << inner_data[1].toString().toInt();
                                         qDebug() << "My list contains" << list.count() << "items.";
                                         qDebug() << "My whole list is" << list;
                    

                    and achieve this, by i didnt solve why

                    My list contains 1 items.
                    My whole list is (14)
                    My list contains 2 items.
                    My whole list is (14, 0)
                    My list contains 3 items.
                    My whole list is (14, 0, 1)
                    My list contains 4 items.
                    My whole list is (14, 0, 1, 13)
                    
                    J 1 Reply Last reply 23 Mar 2021, 07:34
                    1
                    • D deleted286
                      23 Mar 2021, 07:30

                      @JKSH Yes i run it. I realize that, it add the elements into list by one by.
                      I move to the definition of list to my header.

                      run this

                                           list << inner_data[1].toString().toInt();
                                           qDebug() << "My list contains" << list.count() << "items.";
                                           qDebug() << "My whole list is" << list;
                      

                      and achieve this, by i didnt solve why

                      My list contains 1 items.
                      My whole list is (14)
                      My list contains 2 items.
                      My whole list is (14, 0)
                      My list contains 3 items.
                      My whole list is (14, 0, 1)
                      My list contains 4 items.
                      My whole list is (14, 0, 1, 13)
                      
                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 23 Mar 2021, 07:34 last edited by
                      #10

                      @suslucoder said in How to get first element of QList:

                      and achieve this

                      Good job. Now try list.at(0) again.

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

                      D 1 Reply Last reply 23 Mar 2021, 07:38
                      0
                      • J JKSH
                        23 Mar 2021, 07:34

                        @suslucoder said in How to get first element of QList:

                        and achieve this

                        Good job. Now try list.at(0) again.

                        D Offline
                        D Offline
                        deleted286
                        wrote on 23 Mar 2021, 07:38 last edited by deleted286
                        #11

                        @JKSH it gives me the first item but 4 times.

                        Why? I didnt understand this. Becauuse if my inner_data[1] size is 4?
                        How can i solve it?

                        I expect that giving just this

                        My list contains 4 items.
                        My whole list is (14, 0, 1, 13)
                        
                        J J 2 Replies Last reply 23 Mar 2021, 07:41
                        0
                        • D deleted286
                          23 Mar 2021, 07:38

                          @JKSH it gives me the first item but 4 times.

                          Why? I didnt understand this. Becauuse if my inner_data[1] size is 4?
                          How can i solve it?

                          I expect that giving just this

                          My list contains 4 items.
                          My whole list is (14, 0, 1, 13)
                          
                          J Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 23 Mar 2021, 07:41 last edited by
                          #12

                          @suslucoder
                          ok, please post the whole function, from opening { to closing }


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          D 1 Reply Last reply 23 Mar 2021, 08:02
                          0
                          • D deleted286
                            23 Mar 2021, 07:38

                            @JKSH it gives me the first item but 4 times.

                            Why? I didnt understand this. Becauuse if my inner_data[1] size is 4?
                            How can i solve it?

                            I expect that giving just this

                            My list contains 4 items.
                            My whole list is (14, 0, 1, 13)
                            
                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 23 Mar 2021, 07:41 last edited by JKSH
                            #13

                            @suslucoder said in How to get first element of QList:

                            @JKSH it gives me the first item but 4 times.

                            Why?

                            Because you called list.at(0) 4 times, in a loop.

                            Loops are used to run code repeatedly. If you don't want a line of code to run repeatedly, then you must put that line outside the loop.

                            How much do you understand for-loops?

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

                            1 Reply Last reply
                            1
                            • J J.Hilk
                              23 Mar 2021, 07:41

                              @suslucoder
                              ok, please post the whole function, from opening { to closing }

                              D Offline
                              D Offline
                              deleted286
                              wrote on 23 Mar 2021, 08:02 last edited by
                              #14

                              @J-Hilk

                                  void MainWindow::Okuma()
                               {
                              
                              QFile file("readJson/deneme.json");
                              
                              
                                  file.open(QIODevice::ReadOnly | QIODevice::Text);
                              
                                  QByteArray data = file.readAll();
                                  QJsonDocument doc = QJsonDocument::fromJson(data);
                              
                                  QJsonObject root = doc.object();
                                  QJsonArray tlmtArray = root.value("Telemetri Verileri").toArray();
                              
                                  for(int i = 0; i < tlmtArray.size(); ++i)
                                     {
                                         QJsonObject obj = tlmtArray[i].toObject();
                                         QJsonArray gps_array = obj.value("GPS").toArray();
                              
                                  for(int j = 0; j < gps_array.size(); ++j)
                                      {
                                          QJsonObject gps_obj = gps_array[j].toObject();
                              
                                          for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit)
                                          {
                                             // qDebug() << cit.key().toStdU16String() <<  ": (";
                              
                                              QJsonArray inner_data = cit.value().toArray();
                              
                                                  if(inner_data[0] == "float")
                                                  {
                                                     // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                              
                              
                                                  }
                              
                              
                                               if (inner_data[0] == "integer")
                                                  {
                                                 //   qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                                                   list << inner_data[1].toString().toInt();
                              
                                   //                         qDebug() << "My list contains" << list.count() << "items.";
                                   //                         qDebug() << "My whole list is" << list;
                              
                                                   qDebug() << list.at(0);
                                                  }
                              
                                          }
                                      }
                              
                                  }
                              

                              }

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                J.Hilk
                                Moderators
                                wrote on 23 Mar 2021, 08:10 last edited by
                                #15

                                @suslucoder ok, list magically appears, so I assume its a class member, the formatting is horrible,but my guess this is most likely to do copy & past issues in the forum.

                                Place your qDebug here, and it should work fine

                                void Okuma ()
                                {
                                    
                                    QFile file("readJson/deneme.json");
                                    
                                    
                                    file.open(QIODevice::ReadOnly | QIODevice::Text);
                                    
                                    QByteArray data = file.readAll();
                                    QJsonDocument doc = QJsonDocument::fromJson(data);
                                    
                                    QJsonObject root = doc.object();
                                    QJsonArray tlmtArray = root.value("Telemetri Verileri").toArray();
                                    
                                    for(int i = 0; i < tlmtArray.size(); ++i) {
                                        QJsonObject obj = tlmtArray[i].toObject();
                                        QJsonArray gps_array = obj.value("GPS").toArray();
                                        
                                        for(int j = 0; j < gps_array.size(); ++j) {
                                            QJsonObject gps_obj = gps_array[j].toObject();
                                            
                                            for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit) {
                                                
                                                QJsonArray inner_data = cit.value().toArray();
                                                
                                                if(inner_data[0] == "float") {
                                                    // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                                
                                                }
                                                
                                                if (inner_data[0] == "integer") {
                                                    //   qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                                                    list << inner_data[1].toString().toInt();
                                                    
                                                    //                         qDebug() << "My list contains" << list.count() << "items.";
                                                    //                         qDebug() << "My whole list is" << list;
                                                    
                                                }
                                            }
                                        }
                                    }
                                    if(!list.isEmpty())
                                       qDebug() << list.at(0);
                                    else
                                      qDebug() << "List has no elements"
                                }
                                

                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                1
                                • Ketan__Patel__0011K Offline
                                  Ketan__Patel__0011K Offline
                                  Ketan__Patel__0011
                                  wrote on 23 Mar 2021, 08:10 last edited by
                                  #16

                                  @suslucoder said in How to get first element of QList:

                                  QList<int> index;

                                  try this way

                                      QList<int> index;
                                      index.push_back(10);
                                      index.push_back(20);
                                      index.push_back(30);
                                      index.push_back(40);
                                      index.push_back(50);
                                  
                                      qDebug() << index.at(0); /// Answer Is 10
                                  
                                  J 1 Reply Last reply 23 Mar 2021, 08:12
                                  0
                                  • Ketan__Patel__0011K Ketan__Patel__0011
                                    23 Mar 2021, 08:10

                                    @suslucoder said in How to get first element of QList:

                                    QList<int> index;

                                    try this way

                                        QList<int> index;
                                        index.push_back(10);
                                        index.push_back(20);
                                        index.push_back(30);
                                        index.push_back(40);
                                        index.push_back(50);
                                    
                                        qDebug() << index.at(0); /// Answer Is 10
                                    
                                    J Offline
                                    J Offline
                                    JKSH
                                    Moderators
                                    wrote on 23 Mar 2021, 08:12 last edited by
                                    #17

                                    @Ketan__Patel__0011 said in How to get first element of QList:

                                    try this way

                                        QList<int> index;
                                        index.push_back(10);
                                        index.push_back(20);
                                        index.push_back(30);
                                        index.push_back(40);
                                        index.push_back(50);
                                    
                                        qDebug() << index.at(0); /// Answer Is 10
                                    

                                    OP already figured out the function for obtaining the first element: https://forum.qt.io/post/650999 Their current problem is how to avoid calling it inside a loop.

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

                                    1 Reply Last reply
                                    0

                                    10/17

                                    23 Mar 2021, 07:34

                                    • Login

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