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. Remove the only item from a QListWidget

Remove the only item from a QListWidget

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 559 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.
  • P Offline
    P Offline
    p_Each
    wrote on last edited by
    #1

    Hello,

    I am trying to have a button remove the currently selected item from a QListWidget, using

    ui->listWidget->takeItem(ui->listWidget->currentRow());
    

    It works, except for when I attempt to delete the ONLY item in the list: in that case, the program crashes. Is there anything I am missing about the takeItem function?

    Thank you in advance

    JonBJ 1 Reply Last reply
    0
    • P p_Each

      Hello,

      I am trying to have a button remove the currently selected item from a QListWidget, using

      ui->listWidget->takeItem(ui->listWidget->currentRow());
      

      It works, except for when I attempt to delete the ONLY item in the list: in that case, the program crashes. Is there anything I am missing about the takeItem function?

      Thank you in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @p_Each
      You sure it "crashes" when there is one item rather than no items? qDebug() << ui->listWidget->currentRow().

      Otherwise I guess (though surprised) maybe it doesn't like having no item as the current row. I don't know if setCurrentRow(-1) is allowed. Or, are you doing this inside somewhere which expects there to be a current row/at least one list item?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p_Each
        wrote on last edited by
        #3

        I have tried to have the empty list, add the item (I see the element added in the listwidget), and delete it right after that, this is why I assumed the count is 1 when I try to delete it.
        The output of qDebug says currentRow is 0 before I try to delete, which makes sense to me, so I guess I'll look for the issue somewhere else

        JonBJ 1 Reply Last reply
        0
        • P p_Each

          I have tried to have the empty list, add the item (I see the element added in the listwidget), and delete it right after that, this is why I assumed the count is 1 when I try to delete it.
          The output of qDebug says currentRow is 0 before I try to delete, which makes sense to me, so I guess I'll look for the issue somewhere else

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @p_Each

          in that case, the program crashes

          You should run your program in the debugger. When it crashes you should be shown a stack trace, identifying exactly where it is and where it was called from. Maybe it's from a different place which is executed subsequently and expects a non-empty list?

          1 Reply Last reply
          2
          • P Offline
            P Offline
            p_Each
            wrote on last edited by
            #5

            Ok, that was something really stupid on my part, but your comment put me in the right direction.
            I have a QTextEdit in which I show some information about the currently selected list item. When I delete the only item from the list, the currentRow becomes negative and the content of the QTextEdit can't be updated correctly.
            Thanks

            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