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. Track selection in QListWidget after undo/redo operations
Forum Updated to NodeBB v4.3 + New Features

Track selection in QListWidget after undo/redo operations

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 110 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.
  • G Offline
    G Offline
    gabello306
    wrote last edited by
    #1

    I have a QListWidget containing anywhere from 1 to 511 QListWidgetItems. I can add a new item and perform an undo and the new item is removed and a redo to add it back. This is done by saving the state of the vector containing all the QListWidgetItems. I perform the undo/redo by the following:

    QlistWidget->clear();
    

    and then reloading the data from the vector.

    The problem is if there are selected items in the QListWidget. The selected items must be tracked in the undo/redo operations. I do save the selected items into a QList before clearing the QlistWidget. The problem is re-selecting the item after a redo. Since I clear the list and reload it the QListWidgetItem saved doesn't match the new list so it selects the wrong item in the QListWidget.

    I save the selected items by doing the following:

    QList<QListWidgetItems*> selectedIndices = QListWidget->selectedItems();
    

    then clear the QListWidget:

    QListWidget->clear();
    

    reload the QListWidget and then set the selected items:

    for (QListWidgetItems *items : selectedIndices )
    {
        QListWidget->setCurrentItem(items);
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • G gabello306

      I have a QListWidget containing anywhere from 1 to 511 QListWidgetItems. I can add a new item and perform an undo and the new item is removed and a redo to add it back. This is done by saving the state of the vector containing all the QListWidgetItems. I perform the undo/redo by the following:

      QlistWidget->clear();
      

      and then reloading the data from the vector.

      The problem is if there are selected items in the QListWidget. The selected items must be tracked in the undo/redo operations. I do save the selected items into a QList before clearing the QlistWidget. The problem is re-selecting the item after a redo. Since I clear the list and reload it the QListWidgetItem saved doesn't match the new list so it selects the wrong item in the QListWidget.

      I save the selected items by doing the following:

      QList<QListWidgetItems*> selectedIndices = QListWidget->selectedItems();
      

      then clear the QListWidget:

      QListWidget->clear();
      

      reload the QListWidget and then set the selected items:

      for (QListWidgetItems *items : selectedIndices )
      {
          QListWidget->setCurrentItem(items);
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #2

      @gabello306
      But your selectedIndices does not hold indices! Don't save pointers to anything, and not the whole QListWidgetItem. Save indexes, or "unique key" if it's appropriate and unique. I don't know whether with QModelIndexList QListView::selectedIndexes() you can save and reuse a QModelIndex, quite possibly not, but since you have a simple list you can just save an int of its index. Of course you also need to restore sort order if that is dynamic....

      1 Reply Last reply
      2
      • G gabello306 has marked this topic as solved

      • Login

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