On QTreeView selection change, I would like to revert to the previous selection if a certain condition is not met.
For example:
void Editor::treeFolderSelected(QModelIndex const& index)
开发者_运维知识库 {
if(widget) {
if(!widget->trySaveChanges()) {
//revert to previous, validation failed
return;
}
}
//do normal behaviour
}
Currently I am not seeing a straight forward way to do this, as the QModelIndex
does not provide any information regarding what was selected previously.
Does anyone have any suggestions on the best way to implement this?
int lastSelection = -1;
bool abortEvent = false;
void Editor::treeFolderSelected(QModelIndex const& index)
{
if (abortEvent) {
abortEvent = false;
return;
}
if(widget) {
if(!widget->trySaveChanges()) {
if (lastSelection != -1) {
abortEvent = true;
select(lastSelection);
}
return;
}
lastSelection = index;
}
}
The view selection has its own model, QItemSelectionModel
, with signals that give you the new and previous selection.
精彩评论