Results 1 to 6 of 6

Thread: How to remove QListItems via button click?

  1. #1
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default How to remove QListItems via button click?

    Hi,

    I'm providing a QListWidget to get a list of custom values from the user as shown in the figure. The '+' is for adding & '-' is for removing item. To remove item user have to click an item on the list and the click '-' button. But this seems not working. I'm not sure whether I'm using the correct signals to achive this. Please help.

    list.png

    The '-' enabled when user selects an item.

    But click on '-' button do not remove the item from list. I'm keeping the currently selected item in a public variable to pass on remove method.


    Qt Code:
    1. //Signals
    2. connect(btnRemove, SIGNAL(clicked()), this, SLOT(RemoveFromList()));
    3. connect(lstList,SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(OnItemSelected(QListWidgetItem*)));
    4.  
    5.  
    6. //Slots
    7. void Designer::OnItemSelected( QListWidgetItem* item )
    8. {
    9. if ( item != NULL )
    10. {
    11. btnRemove->setEnabled(true);
    12. p_CurrentItem = item; //this holds the user clicked item
    13. }
    14. }
    15.  
    16. void Designer::RemoveFromList()
    17. {
    18. if ( p_CurrentItem != NULL )
    19. lstList->removeItemWidget(p_CurrentItem); //this don't remove the item, it stay in the list
    20.  
    21. if ( !lstList->count() )
    22. btnRemove->setEnabled(false);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 21st September 2011 at 09:36. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to remove QListItems via button click?

    removeItemWidget? you want to remove an item not a widget! Just skip "Widget".

    EDIT: and no need to save the selected item. You can get that every time by using QListWidget::currentItem().

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to remove QListItems via button click?

    You can use takeItem (make sure you manually delete the returned pointer) or just call delete on the item pointer.
    Qt Code:
    1. void Designer::RemoveFromList()
    2. {
    3. if ( p_CurrentItem != NULL )
    4. delete p_CurrentItem; //this will remove the item
    5. //...
    To copy to clipboard, switch view to plain text mode 
    //that ItemWidget removes something else, see here.

  4. #4
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to remove QListItems via button click?

    I could not find such method removeItem in QListWidget and code is not compiling. I'm using the latest Qt version.


    Added after 14 minutes:


    Isn't there any method to do this in one shot? To takeItem I have find the current row and pass it to get selected item.
    Last edited by IndikaU; 21st September 2011 at 10:10.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to remove QListItems via button click?

    You already have the pointer, so just call delete

  6. #6
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to remove QListItems via button click?

    Thanks Zlatomir, I'm using the way u proposed.

Similar Threads

  1. Remove max,min button on a QMainWindow
    By Krish_ng in forum Qt Programming
    Replies: 9
    Last Post: 3rd August 2012, 13:36
  2. Replies: 2
    Last Post: 26th April 2011, 11:44
  3. how to remove tab with closable button.
    By ishkabible in forum Newbie
    Replies: 8
    Last Post: 20th September 2010, 02:03
  4. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 14:27
  5. Remove restore button
    By vermarajeev in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2007, 13:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.