I have two separate threads. First thread for GUI, and second for application data.
Initially, I wanted to use QUndoStack and QUndoView.
But there was a problem - this view works directly with the stack:
In this case I got race condition.
To solve this problem I wrote custom myUndoView using QListView and QAbstractListModel. Now all my slots using queued connections and I store a lightweight copy of the "real" undo stack in the custom view model. This is same size and same order of the "real" undo stack elements. A lightweight element contains only type of the undo command and text.
Now I have another problem. I'm not blame for this ))
I have a QLineEdit that emits signal on value changed when I click Enter key or lost focus. This value in turn is sent to object (app model) with "real" undo stack. It works.
But this does not work when I interact with undo view too. Repeat, I'm not blame for this. QUndoView has the same behavior.
Step by step:
- QLineEdit in focus.
- Changing value, still in focus.
- Click the mouse in the undo view.
Oops.. currentIndexChanged() signal from undo view can be sent first, or signal from QLineEdit can be sent first.
It always differs ..
If signal from QLineEdit was sent first - it works correctly. The history of changes not lost.
I want to make enter/blur and other changes (not in history view) always invoked first. Probably I can use QTimer::singleShot() for delay of emit undo view signals . But not curentIndexChanged() because this signal emit with user interactions and when undo stack updated programmatically. We can not determine who make changes - user or application.
What I tried?
Intercept mouse clicks:
myUndoView::mousePressEvent(QMouseEvent *event)
{
event->ignore();
qDebug() << "catched!";
}
But sometimes it loses the clicks. At the bottom of the list item (under the letters) is an area that pass a click to the item. This may be a Qt bug, found in my environment: Debian, Mate, GTK+ Qt-style.
I think, I can place another transparent widget over list, and get coordinates of the click and use it:
http://doc.qt.io/qt-5/qabstractitemview.html#indexAt
to get the selected index.
Or I make all wrong? Maybe there is an easier way?
How to make it right?