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. QListWidget remove duplicates
QtWS25 Last Chance

QListWidget remove duplicates

Scheduled Pinned Locked Moved General and Desktop
duplicateqt5beginnerqlistwidget
4 Posts 2 Posters 6.9k 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.
  • V Offline
    V Offline
    Votato
    wrote on 16 Aug 2015, 18:06 last edited by
    #1

    Hello.
    I am new to QT and I'm not sure how to get rid of duplicates in a QListWidget.

    I have the following code:

    for (i=0;i<fileNames.length();i++)
    if (fileNames[i] != temp)
    {
        ui->listWidget->addItem(fileNames[i]);
    }
    

    fileNames is a QStringList from QFileDialog. The temp variable is a QString that stores a QUrl.ToString() as a hack to something.

    My question is how can I prevent duplicates from happening in the QListWidget? I tried looping through it, unsuccessfully. It's probably me being an idiot. :D

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 16 Aug 2015, 18:23 last edited by
      #2

      Hi, welcome to devnet.

      A simple preprocessing of the list should do:

      fileNames.removeDuplicates();
      fileNames.removeOne(temp);
      ui->listWidget->addItems(fileNames);
      
      V 1 Reply Last reply 19 Aug 2015, 16:26
      0
      • C Chris Kawa
        16 Aug 2015, 18:23

        Hi, welcome to devnet.

        A simple preprocessing of the list should do:

        fileNames.removeDuplicates();
        fileNames.removeOne(temp);
        ui->listWidget->addItems(fileNames);
        
        V Offline
        V Offline
        Votato
        wrote on 19 Aug 2015, 16:26 last edited by
        #3

        @Chris-Kawa I'm not actually sure how to do the first part. The trouble is that the program allows the user to select files to copy, and the user can select them more than once using the dialog.

        This means that there might be the occasion where listWidget already has the entry and the user is trying to add it again by accident. I need to compare both fileNames and listWidget and check if they have any duplicates.

        1 Reply Last reply
        0
        • C Online
          C Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on 19 Aug 2015, 17:32 last edited by Chris Kawa
          #4

          Then it's just a matter of removing from the list items that are already added. Something like this:

          //get the list from somewhere
          QStringList fileNames = ...
          
          //remove the temp item you mentioned
          fileNames.removeOne(temp);
          
          //remove the items already in the widget
          int numItems = listWidget->count();
          for(int i = 0; i < numItems; ++i)
              fileNames.removeOne(listWidget->item(i)->text());
          
          //now that the list is "clean" all there is left to do is add the new items
          listWidget->addItems(fileNames);
          
          1 Reply Last reply
          0

          4/4

          19 Aug 2015, 17:32

          • Login

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