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

How to get first element of QList

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5qlist
17 Posts 4 Posters 5.8k 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 deleted286

    @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

    JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on 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
    1
    • JKSHJ JKSH

      @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 last edited by
      #5

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

      JKSHJ 1 Reply Last reply
      0
      • D deleted286

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

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on 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
        1
        • JKSHJ JKSH

          @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 last edited by
          #7

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

          JKSHJ 1 Reply Last reply
          0
          • D deleted286

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

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on 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
            0
            • JKSHJ JKSH

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

              D Offline
              D Offline
              deleted286
              wrote on 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)
              
              JKSHJ 1 Reply Last reply
              1
              • D deleted286

                @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)
                
                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on 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
                0
                • JKSHJ JKSH

                  @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 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.HilkJ JKSHJ 2 Replies Last reply
                  0
                  • D deleted286

                    @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.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on 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
                    0
                    • D deleted286

                      @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)
                      
                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 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.HilkJ J.Hilk

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

                        D Offline
                        D Offline
                        deleted286
                        wrote on 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.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on 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 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
                            
                            JKSHJ 1 Reply Last reply
                            0
                            • Ketan__Patel__0011K Ketan__Patel__0011

                              @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
                              
                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on 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

                              • Login

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