Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get first element of QList



  • 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;
                           }
    

  • Moderators

    @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.



  • @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


  • Moderators

    @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.



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


  • Moderators

    @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;
    


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


  • Moderators

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



  • @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)
    

  • Moderators

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

    and achieve this

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



  • @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)
    

  • Moderators

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


  • Moderators

    @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?



  • @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);
                        }
    
                }
            }
    
        }
    

    }


  • Moderators

    @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"
    }
    


  • @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
    

  • Moderators

    @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.


Log in to reply