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.4k 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_IrudezuR Offline
    R_IrudezuR Offline
    R_Irudezu
    wrote on 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.

    RatzzR 1 Reply Last reply
    0
    • R_IrudezuR R_Irudezu

      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.

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on 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_IrudezuR 1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on 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_IrudezuR 1 Reply Last reply
        2
        • dheerendraD dheerendra

          Did you check at

          QListWidgetItem * QListWidget::item(int row)
          

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

          R_IrudezuR Offline
          R_IrudezuR Offline
          R_Irudezu
          wrote on 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
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on 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
            • RatzzR Ratzz

              @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_IrudezuR Offline
              R_IrudezuR Offline
              R_Irudezu
              wrote on 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

              • Login

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