Is it possible to get last x items (such as 5 items) from QListWidget?

  • I put a QListWidget into UI and i am creating QListWidgetItems at runtime. How can i get last 5 items from widget. 5 is just a number, i mean a specific number of items...

    Let me show you how i wrote some code:

    QDirIterator it("C:/Users/a_path_to_a_file", QDirIterator::Subdirectories);;  // Pass first..;  // .. two dots
    for(int i = 0; i < 100; i++)
        QString jpegItemFile =;
        if(jpegItemFile .contains("jpg", Qt::CaseInsensitive))
            QListWidgetItem *item = new QListWidgetItem(QIcon(jpegItemFile ), NULL);  //NULL for icon name disable

    So i am adding some icon pictures to widget, i want to get recently added items, but not with clicking items, just by code.

  • Qt Champions 2017

    Did you check at

    QListWidgetItem * QListWidget::item(int row)

    Given a row it will help you to fetch the Item.

  • @dheerendra Yes i checked but couldn't achieved get the latest added items, because list is updating continuous

  • Qt Champions 2017

    If updated and fetch happens at same time, it is difficult to get the last. Question of what is last will be difficult to decide. There should be time interval when you need to get the last.

  • @Ratzz Sorry i can not check your answer as solved answer, Qt forum has a bug about that. I solved my problem, i've already read the topic that you linked here but after you posted it then a read again and i solved my problem thank you, but even so trying to give more explanatory answers is a good way because i like this forum and i want to see answers from another angles, another perspectives. When i get experienced at Qt i want to give good answers too. :)

    And thank you so much Mr. @dheerendra . I think approach with row is a better way but i was need to a quick way so that stackoverflow link helped me about that.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.