[solved] Best way to activate the correct QUndoStack

  • Hello!

    I am writing some application which has a QTreeView and a QMdiArea. Inside that QMdiArea are Widgets having a QUndoStack which are managed by a QUndoGroup. In addition when clicking/selecting some area inside those Widgets another Widget is shown, holding additional information for that last clicked element.

    As long as I have a QUndoStack just for those Widgets inside the QMdiArea eveything works fine and the QUndoStacks are managed fine by QUndoGroup.

    Now I tried to add another QUndoStack to that QTreeView and here my problem starts.

    Currently I am using focusInEvent() to switch to the QUndoStack of the QTreeView. But what is the best way to switch back to the QUndoStack of those QWidgets inside that QMdiArea? There is no problem when the user clicks on that QWidgets, I can handle that with focusInEvent() too. But when the user clicks on those windows with additional information, it seems that I need focusInEvent() for every single element in those windows which seems to be a lot of typing and seems to be error-prone (simple because more code and because it may be forgotten).

    Another idea was to use focusOutEvent() in the QTreeView, but that would mean that the menubar which shows undo/redo Actions would switch the focus of these Undo/Redo Actions too, causing unexpected behavior.

    I think that enterEvent() in the QMdiArea is not a good idea, the user might click on the QTreeView and simple move the mouse somewhere, that shall not switch to a QUndoStack of a different context.

    Any better ideas?

    BTW: I use a QMainWindow (with the QMdiArea) inside another QMainWindow (which hold that QTreeView) because of requirements for docking elements.

  • Lifetime Qt Champion


    You can use an eventFilter in your QMdiArea and react on the focus event of all the widgets in there

    Hope it helps

  • QMdiArea::focusInEvent() is called only when I click into the gray background, not when clicking on some Widget in it :-( And it is also not called for dockable widgets.

    And yes, setFocusPolicy(Qt::WheelFocus); gets called in its constructor ((Qt::WheelFocus might be to much, will change this later …))

  • Lifetime Qt Champion

    That I understood, that's why I suggested "eventFilter":http://doc.qt.io/qt-5/qobject.html#eventFilter you make your QMdiArea the filter for all the widgets inside it and then you can react on the focusIn event of all of them

  • Hmm ... oops … okay … will try that tomorrow, now it is time for a good beer ;^)

  • Did take a little bit longer, but works now.

    The main difference is subclassing, which I do not need now.


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.