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 delete selected item
QtWS25 Last Chance

QListWidget delete selected item

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistwidgetqlistwidgetitemdelete rowdeleteremoverow
7 Posts 6 Posters 32.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Welcome, how can I delete selected item in list? I try something like this but it doesn't seem to work.

    void MyWindow::on_pushButtonDelete_clicked()
    {
        // Delete selected item in list
        ui->listWidgetMy->currentItem()->takeItem();
    }
    

    Thank you for detailed answers.

    joeQJ 1 Reply Last reply
    1
    • DPalhariniD Offline
      DPalhariniD Offline
      DPalharini
      wrote on last edited by
      #2

      You may use this code to remove:

          QListWidgetItem *it = ui->listWidget->takeItem(ui->listWidget->currentRow());
          delete it;
      

      Keep in mind that "takeItem" does not delete the object.

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        currentItem() is not necessarily selected. for example: if you select items 2,4,5 and then deselect item 4 currentItem() will be 4 but the selected items are 2 and 5

        To remove the row completely:

        QModelIndexList selectedList = ui->listWidgetMy->selectionModel()->selectedIndexes(); // take the list of selected indexes
        std::sort(selectedList.begin(),selectedList.end(),[](const QModelIndex& a, const QModelIndex& b)->bool{return a.row()>b.row();}); // sort from bottom to top
        for(const QModelIndex& singleIndex : selectedList)
        ui->listWidgetMy->model()->removeRow(singleIndex.row()); // remove each row
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        K 1 Reply Last reply
        5
        • ? A Former User

          Welcome, how can I delete selected item in list? I try something like this but it doesn't seem to work.

          void MyWindow::on_pushButtonDelete_clicked()
          {
              // Delete selected item in list
              ui->listWidgetMy->currentItem()->takeItem();
          }
          

          Thank you for detailed answers.

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by
          #4

          @testerius

          Hi, friend, welcome devnet.

          if i were you. i will following the steps to find my answer.

          1. I used the QListWidget. I will search QListWidget in Qt help manual.
          2. If i want to delete the selected item, i must get them first. so, i search select key in manual.or function list. i find the followinginfo.
          selectedItems() const : QList<QListWidgetItem *>
          //Returns a list of all selected items in the list widget.
          
          1. Then, i must to delete or remove them form QListWidget. so, i search the 'delete' or 'remove' key in manual. Nothing? Why didn't have the 'delete item' or 'remove item' function? Think...Think...I watched all functions of QListWidget. I found takeItem.
          QListWidgetItem *QListWidget::takeItem(int row)
          /*Removes and returns the item from the given row in the list widget; otherwise returns 0.
          Items removed from a list widget will not be managed by Qt, and will need to be deleted manually.
          See also insertItem() and addItem().
          
          1. Now, I got the list of items i want to delete, and, i take every it in for. **But Note, I should delete it. **
          for(...){
              takeItem(item);
              delete item;
          }
          

          This is my way how to find the answer to shared for you.

          There are ways, There is one question. we should to learn the ways not to remeber the answer.

          Just do it!

          1 Reply Last reply
          1
          • Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #5
            foreach (QListWidgetItem *NAME, ui->listWidget->selectedItems()) {
               delete ui->listWidget->takeItem(ui->listWidget->row(NAME));
            }
            

            Do what you want.

            1 Reply Last reply
            0
            • VRoninV VRonin

              currentItem() is not necessarily selected. for example: if you select items 2,4,5 and then deselect item 4 currentItem() will be 4 but the selected items are 2 and 5

              To remove the row completely:

              QModelIndexList selectedList = ui->listWidgetMy->selectionModel()->selectedIndexes(); // take the list of selected indexes
              std::sort(selectedList.begin(),selectedList.end(),[](const QModelIndex& a, const QModelIndex& b)->bool{return a.row()>b.row();}); // sort from bottom to top
              for(const QModelIndex& singleIndex : selectedList)
              ui->listWidgetMy->model()->removeRow(singleIndex.row()); // remove each row
              
              K Offline
              K Offline
              kishore_hemmady
              wrote on last edited by
              #6

              @VRonin
              what if i want to select the multiple indexes of the listWidget to delete,
              and how can i achieve this by selecting the check Box rather than selecting the list content?

              VRoninV 1 Reply Last reply
              0
              • K kishore_hemmady

                @VRonin
                what if i want to select the multiple indexes of the listWidget to delete,
                and how can i achieve this by selecting the check Box rather than selecting the list content?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @kishore_hemmady said in QListWidget delete selected item:

                how can i achieve this by selecting the check Box rather than selecting the list content?

                for(int i=ui->listWidget->model()->rowCount()-1;i>=0;--i){
                if(ui->listWidget->model()->index(i,0).data(Qt::CheckStateRole).toInt()==Qt::Checked)
                ui->listWidgetMy->model()->removeRow(i);
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                5

                • Login

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