I'm usually used to the .net framework and recently got the chance to work on a project using C++/Qt. In this context, I want to implement the following functionality: (to simplify things lets say I have a ListView and a Textbox/Textedit)
Basically I see two solutions (there may be more) to this:
aboutToChangeSelection(proposedSelection, vetoObj)
changedSelection(newSelection)
The first signal would be sent to the textedit, which would then eventually make use of its veto. After this, the same signal would be sent to the list itself, do the action depending of the veto and send the second signal if it actually changed the selection.
In the .NET world there exists such a mechanism, with the help of CancelEventArgs
. I know that .NET events and Qt signals have quite a different working principle, but is there any Qt equivalent to archieve the same effect?
Thanks a lot for any help!
If you are using a QListWidget
instead of a QListView
, then you can do the following:
QListWidget::itemPressed ( QListWidgetItem * item )
rather than the signal saying the item has changed (ie currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous )
in my example )currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous )
which will be used only to remember the current and last item.Now there is one problem. Which signal will be received first?? Not specified in the doc, and it can be in any order. If a user is on item A
, and clicks on item B
, you want lastitem to be A
, and current item to be B
at the moment you are handling itemPressed
. So you want to handle currentItemChanged
first. Fortunately you can do use Qt::DirectConnection
for currentItemChanged
and use Qt::QueuedConnection
for itemPressed
ALTERNATIVE:
You can use an event filter on the list widget. This filter will be the one to perform the processing you described in your few steps. If the user clicks accepts, you send the event to the list view. If the user reject, you block the event. I am not sure, but it is possible that the events are not processed in a filter which will make this alternative non-viable.