PDA

View Full Version : How to ignore current item change in QListWidget?



GNU/Varan
13th May 2011, 10:52
The question as well relates to other QAbstractItemView derivates.
I want to check some condition if I go to new current item. If it fails - then nothing should happen, and current item should remain the same.

wysota
13th May 2011, 12:26
Very good. And?

GNU/Varan
13th May 2011, 12:31
Very good. And?
I don't know how to do this. I can call QListWidget::setCurrentItem(prev) in the QListWidget::currentItemChanged(QListWidgetItem * curr, QListWidgetItem * prev), but this will lead to a recursion of some kind. Is there any other way?

wysota
13th May 2011, 12:47
If you provide a stop condition, you will prevent recursing.

GNU/Varan
13th May 2011, 14:07
If you provide a stop condition, you will prevent recursing.

Ok. It's not rather elegant, though, if I have two nested levels of lists and the condition is on the ground level...

Moreover, the top level is QTreeView...

GNU/Varan
17th May 2011, 09:46
By the way - how to deal with selection? It's independent of current item so I run into situation when the current item is right whereas the selected item is wrong.

wysota
17th May 2011, 10:31
How do you want to "deal" with it? You have the selection model and you can manipulate it as you want.

GNU/Varan
17th May 2011, 10:52
But in what function must I manipulate it? I do something like


ui.subprograms->setCurrentItem(prev);
ui.subprograms->selectionModel()->select(ui.subprograms->currentIndex(), QItemSelectionModel::SelectCurrent);

or even


ui.subprograms->setCurrentItem(prev);
prev->setSelected(true);

in my "currentItemChanged"-handler but this has no effect. Seems like selection automatically changes after the current item.
Should I override
void QAbstractItemView::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected ) as well?

wysota
17th May 2011, 11:10
You should connect to QItemSelectionModel's selectionChanged() signal.

yren
19th July 2011, 19:42
I had the same problem as yours. It seems like QT is not capable of handling this situation. Since the actual change of the selection happens after the slot returns, and while in the slot, the prev item is still the "current" item, so setCurrentItem(prev) in the slot won't do anything new. In MFC, you can set a flag bit in the event to tell the system not to set the selection change so the old selection will still be valid after the event handler returns.