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. Is it possible to get last x items (such as 5 items) from QListWidget?

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

Scheduled Pinned Locked Moved Solved General and Desktop
qlistwidgetqlistwidgetitem
6 Posts 3 Posters 2.3k 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.
  • R Offline
    R Offline
    R_Irudezu
    wrote on 23 Nov 2018, 06:09 last edited by R_Irudezu
    #1

    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:

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

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

    Keizoku wa chikaranari.

    R 1 Reply Last reply 23 Nov 2018, 06:35
    0
    • R R_Irudezu
      23 Nov 2018, 06:09

      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:

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

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

      R Offline
      R Offline
      Ratzz
      wrote on 23 Nov 2018, 06:35 last edited by
      #2

      @R_Irudezu
      May be this way helps you ?
      https://stackoverflow.com/questions/25459584/how-can-fetch-the-row-which-has-been-recently-added-in-the-qlistwidget

      --Alles ist gut.

      R 1 Reply Last reply 23 Nov 2018, 10:32
      1
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 23 Nov 2018, 06:59 last edited by
        #3

        Did you check at

        QListWidgetItem * QListWidget::item(int row)
        

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        R 1 Reply Last reply 23 Nov 2018, 07:30
        2
        • D dheerendra
          23 Nov 2018, 06:59

          Did you check at

          QListWidgetItem * QListWidget::item(int row)
          

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

          R Offline
          R Offline
          R_Irudezu
          wrote on 23 Nov 2018, 07:30 last edited by
          #4

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

          Keizoku wa chikaranari.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dheerendra
            Qt Champions 2022
            wrote on 23 Nov 2018, 07:45 last edited by
            #5

            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.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            2
            • R Ratzz
              23 Nov 2018, 06:35

              @R_Irudezu
              May be this way helps you ?
              https://stackoverflow.com/questions/25459584/how-can-fetch-the-row-which-has-been-recently-added-in-the-qlistwidget

              R Offline
              R Offline
              R_Irudezu
              wrote on 23 Nov 2018, 10:32 last edited by
              #6

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

              Keizoku wa chikaranari.

              1 Reply Last reply
              0

              4/6

              23 Nov 2018, 07:30

              • Login

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