Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Remove the only item from a QListWidget

    General and Desktop
    2
    5
    228
    Loading More Posts
    • 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
      p_Each last edited by

      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

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @p_Each last edited by JonB

        @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 Reply Quote 0
        • P
          p_Each last edited by

          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

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @p_Each last edited by JonB

            @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 Reply Quote 2
            • P
              p_Each last edited by

              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 Reply Quote 0
              • First post
                Last post