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. QTreeWidget and QUndoStack?
Qt 6.11 is out! See what's new in the release blog

QTreeWidget and QUndoStack?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 9.4k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    olihey
    wrote on last edited by
    #1

    Hej,

    I have subclassed a QTreeWidget for showing a structure to the user.
    The user is also able to drag items in that tree around (only Qt::MoveAction). Works fine.

    Now, I also have a QUndoStack to provide the option to undo things.

    Question is now: How can I offer undo for the (automatic) move actions in the QTreeWidget?

    Thanks,
    Oli

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yshurik
      wrote on last edited by
      #2

      I would recommend to use model - view idea when undo is needed. Implement basics actions - add/remove/move/change of your model, then subclass the model to "gui" class and reimplement these actions to wrap them with undo commands - you will have well structured, easy to modify and easy to change behavior if needed

      1 Reply Last reply
      0
      • O Offline
        O Offline
        olihey
        wrote on last edited by
        #3

        I was afraid that I need to do that!

        After playing around for days trying to solve that issues in a nice way, I think I need to find the courage and subclass a QAbstractItemView.

        To bad, QTreeWidget is fine ... except the missing undo functionality!

        Thanks,
        Oli

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mariusbu
          wrote on last edited by
          #4

          You should only need to subclass "QAbstractItemModel":http://doc.qt.nokia.com/4.6/qabstractitemmodel.html. "QStandardItemModel":http://doc.qt.nokia.com/4.6/qstandarditemmodel.html is also worth a look.

          Cheers,
          Marius Bugge Monsen (mbm)

          1 Reply Last reply
          0
          • O Offline
            O Offline
            olihey
            wrote on last edited by
            #5

            Hej,

            Me again, just wanted to know if my current solution for this is okay ;)

            I now have subclassed QTreeView and QAbstractItemView.
            To get the undo function to work I now intercept QDropEvents in overwriting QTreeView::dropEvent(). That way I just push a new QUndoCommand to the undo stack (that performs the reparenting in the tree) and set the QDropEvent::dropAction to IgnoreAction before calling QTreeView::dropEvent() of my super class.

            Is that the right way to do it?

            Oli

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yshurik
              wrote on last edited by
              #6

              Looks okay for now - main issue for you to get it working :)

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hejibo
                wrote on last edited by
                #7

                Have you solved the problem? I want to implement redo/undo for QTableWidget. Could I see you solution? Thanks.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  olihey
                  wrote on last edited by
                  #8

                  Hej,

                  Yes I solved the problem. But I had to go from the QTreeWidget to the QTreeView because the QTreeWidget did a lot of things automatically I didn't want and I couldn't intercept!

                  So I created a subclass from QAbstractItemModel to represent my hierarchical structure and show this using my own subclass of QTreeView. In that subclass of QTreeView I overwrote the dropEvent method to intercept what ever the QTreeView is doing and instead doing the reparenting myself by creating a QUndoCommand (subclass) and put it in the QUndoStack!

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    yshurik
                    wrote on last edited by
                    #9

                    You may find this helpful regarding implementing undo: "http://lynxline.com/undo-in-complex-qt-projects/":http://lynxline.com/undo-in-complex-qt-projects/

                    1 Reply Last reply
                    0

                    • Login

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