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. QListWidgetItem removal does not work for the last element

QListWidgetItem removal does not work for the last element

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.1k 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.
  • B Offline
    B Offline
    BartoszT
    wrote on last edited by
    #1

    This is my code:

    void MainWindow::on_pushButtonDELETE_clicked()
    {
        QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
        foreach(QListWidgetItem * item, items)
        {
            delete ui->listWidget->takeItem(ui->listWidget->row(item));
        }
    }
    

    There is no NULL pointer passed to the ui->listWidget->takeItem.

    It removes item on the list but when there is one item on the list, removing causes"Invalid parameter passed to C runtime function" and program crashes.
    Where is the problem of that?

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Tried the same code in Qt5.13 with a clean project and a ListWidget and nothing
      bad happened.
      Just deleted the single line. I tried adding many and delete some of them, then the last.
      All cases worked.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What OS are you running ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BartoszT
          wrote on last edited by
          #4

          Win7 Qt 5.9.2

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Did you try the same code in clean project to validate its actually that?

            1 Reply Last reply
            1
            • B BartoszT

              This is my code:

              void MainWindow::on_pushButtonDELETE_clicked()
              {
                  QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
                  foreach(QListWidgetItem * item, items)
                  {
                      delete ui->listWidget->takeItem(ui->listWidget->row(item));
                  }
              }
              

              There is no NULL pointer passed to the ui->listWidget->takeItem.

              It removes item on the list but when there is one item on the list, removing causes"Invalid parameter passed to C runtime function" and program crashes.
              Where is the problem of that?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @BartoszT
              Although your code is actually supposed to be probably OK, especially since @mrjj says it worked fine for him, there occurs to me small possibility. You are deleting all items on list, just maybe the foreach has an issue moving off the last one it has deleted because there are no remaining items. As I say I doubt it, but why not try deleting by, say, index instead? Along the lines of:

              int num = items.count();
              for (int i = 0; i < num; i++)
                  delete items.takeAt(0);   //Always delete element 0
              

              Any difference in behaviour?

              Separately, make sure your own code does not have, say, a slot on list item removal which relies on there still being one selected item and crashes if not? If you can, running it inside debugger should show you where this error/crash is emanating from.

              1 Reply Last reply
              2
              • B Offline
                B Offline
                BartoszT
                wrote on last edited by
                #7

                Thanks for the advice,
                I also use the on_listWidget_currentRowChanged slot, after deleting the last item in the list, the current currentRow number assumes the value -1 and it causes a problem. The list is linked to the pointers container and the data is fetched from the object associated with the pointer.

                1 Reply Last reply
                1
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @BartoszT said in QListWidgetItem removal does not work for the last element:

                  currentRowChanged

                  QItemSelectionModel::currentRowChanged() will emit an invalid QModelIndex when there is nothing selected so -1 is correct here.
                  Please show us a minimal compilable example since noone except you can reproduce a problem here.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • B Offline
                    B Offline
                    BartoszT
                    wrote on last edited by
                    #9

                    For this additional one comment, program crashes.
                    Without comment, program works.

                    0_1563371761908_prog.png

                    JonBJ 1 Reply Last reply
                    0
                    • B BartoszT

                      For this additional one comment, program crashes.
                      Without comment, program works.

                      0_1563371761908_prog.png

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @BartoszT
                      Yes, well (with the check line commented out) going TachoList.at(-1) is going to be the source of your ""Invalid parameter passed to C runtime function" and program crashes" problem!

                      @Christian-Ehrlicher has said currentRow can indeed be -1, so it's your responsibility to deal with that in whatever code you add.

                      Which is why I originally wondered whether you had code elsewhere relying on there being at least one selected item :)

                      1 Reply Last reply
                      2

                      • Login

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