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. How to revert to previous selection when an undo operation happens in QTreeView
Forum Updated to NodeBB v4.3 + New Features

How to revert to previous selection when an undo operation happens in QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 532 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
    garcia_g
    wrote on last edited by
    #1

    Hi,

    I'm quite new to Qt. So in my current GUI, undoing an operation doesn't return selection back to the previously selected one, which means the previous one doesn't get highlighted.

    For example, if I select one part of the model, do some operations, then select another part and undo it, the selection doesn't go back to the previous one, so it must be selected again before I can do other stuff.

    JonBJ 1 Reply Last reply
    0
    • G garcia_g

      Hi,

      I'm quite new to Qt. So in my current GUI, undoing an operation doesn't return selection back to the previously selected one, which means the previous one doesn't get highlighted.

      For example, if I select one part of the model, do some operations, then select another part and undo it, the selection doesn't go back to the previous one, so it must be selected again before I can do other stuff.

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

      @garcia_g
      Hello and welcome.

      So how do you do your "undo" operation? If you want, say, selection to revert to what it was before your "operations" then you must remember what was selected and reselect that as part of the "undo".

      1 Reply Last reply
      1
      • G Offline
        G Offline
        garcia_g
        wrote on last edited by
        #3

        Let's say I rotated the selected item and now I want to undo this rotation. If I press "ctrl+z" to undo, then the item is deselected, i.e. "un-highlighted", and my selected item is null in this case. So how can I remain the item selected even when the "undo" operation happens such that it is the same as if I had clicked on it?

        JonBJ 1 Reply Last reply
        0
        • G garcia_g

          Let's say I rotated the selected item and now I want to undo this rotation. If I press "ctrl+z" to undo, then the item is deselected, i.e. "un-highlighted", and my selected item is null in this case. So how can I remain the item selected even when the "undo" operation happens such that it is the same as if I had clicked on it?

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

          @garcia_g
          QTreeView has QItemSelectionModel *QAbstractItemView::selectionModel() const. And that has e.g. void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command). So if you remember QModelIndexList QTreeView::selectedIndexes() const you can use that to re-select.

          See also e.g. https://stackoverflow.com/questions/4967660/selecting-a-row-in-qtreeview-programmatically or https://forum.qt.io/topic/11170/solved-how-to-set-an-item-in-a-qtreeview-as-selected.

          While you are implementing this. Are you aware that Qt has a QUndoStack Class? You might want to use this. It does not provide the undo "operations" --- e.g. adding/deleting/selecting your elements --- you still have to write the code for that, just as now. But it gives you the infrastructure for managing the undo/redo operations, especially if you will allow multi-action undo/redo, which otherwise you will have to write yourself.

          1 Reply Last reply
          3

          • Login

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