Results 1 to 3 of 3

Thread: QListWidget items swapping

  1. #1
    Join Date
    Nov 2020
    Qt products

    Question QListWidget items swapping


    I am trying to swap the content of a QListWidget. Perhaps I am not using the right methods ... or maybe I need to use the listview + model instead ?

    The context here is that I have a plotter and I am loading multple files. Each files has its own tab with the plotted data. Each tab contains an object with the filename, channelList, etc ..

    The "Channel list" is in a general QListWidget and I would like to switch the content of that widget when I switch tab.

    Each dataLog object contains the list of channels with a QList that contains items property:
    Qt Code:
    1. QList<QListWidgetItem*> itemList;
    To copy to clipboard, switch view to plain text mode 

    And here's the handler when I change tabs :

    Qt Code:
    1. void MainWindow::on_tabWidget_logs_currentChanged(int index)
    2. {
    3. foreach(logData* l, this->openedLogs)
    4. {
    5. if(l->parentWidget == ui->tabWidget_logs->widget(index))
    6. {
    8. int count = ui->listWidget_channels->count();
    10. //currentLog->itemList.clear(); // This actually deletes the items in the dataLog object so it's not good
    12. // This doesnt seem to be working since all the items are still in the QListWidget
    13. for ( int i=0; i<count; i++)
    14. {
    15. ui->listWidget_channels->takeItem(i);
    16. }
    18. currentLog = l;
    19. // Take the items from the log object and populate the QListWidget with them
    20. foreach(QListWidgetItem *i, currentLog->itemList)
    21. {
    22. ui->listWidget_channels->addItem(i);
    23. }
    24. ui->listWidget_channels->sortItems();
    25. break;
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 773 Times in 763 Posts
    Qt products
    Windows Android

    Default Re: QListWidget items swapping

    The problem with your code to remove the items from listWidget_channels is that as soon as you take one item out, "count" is no longer valid. Try this:

    Qt Code:
    1. while ( ui->listWidget_channels->count() > 0 )
    2. ui->listWidget_channels->takeItem( 0 );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2020
    Qt products

    Thumbs up Re: QListWidget items swapping

    Geez ! I really missed that one .. thanks.

    I was aware of one bad thing and it always catch me ... the decreasing for loop when the count was in the if statement :
    Qt Code:
    1. for (int i=0; i< ui->listWidget_channels->count(); i++ ){ ...
    To copy to clipboard, switch view to plain text mode 

    But indeed, having a variable with count before taking items is still wrong.

    Thanks again !

Similar Threads

  1. Move items up and down in QListWidget
    By araglin in forum Newbie
    Replies: 7
    Last Post: 31st August 2016, 12:05
  2. Replies: 0
    Last Post: 1st June 2016, 13:19
  3. Replies: 2
    Last Post: 1st April 2011, 10:32
  4. Qt 4.2: QListWidget changes size of its items
    By KingFish in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 12:06
  5. Getting all items of a QListWidget
    By Codepoet in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2006, 23:52

Tags for this Thread


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.