Selecting
I ran into something peculiar today when using the Model/View classes in Qt 4.2 - probably something related to TaskTracker issue 143383. To demonstrate, let's start with a minimal model that can be reset from a public method:
class MyStringListModel : public QStringListModel
{
public:
MyStringListModel( const QStringList &items ) : QStringListModel( items, 0 ) {}
void resetModel() { reset(); }
};
I start by initializing the model and viewing it through a QListView:
QListView listView;
QStringList items;
items << "Kalle" << "Olle" << "Albert" << "Sven" << "Anders" << "Markus";
MyStringListModel model( items );
listView.setModel( &model );
Then I set the current index and select it - this is equvalent to picking it with the mouse (that was how I ran into the issue):
listView.selectionModel()->setCurrentIndex(
model.index( 3, 0, QModelIndex() ),
QItemSelectionModel::SelectCurrent );
This gives us the following state:
listView.selectionModel()->hasSelection() == true
listView.selectionModel()->selectedIndexes().count() == 1
listView.selectionModel()->currentIndex().isValue == true
Now, I reset the model:
model.resetModel();
The situation changes to:
listView.selectionModel()->hasSelection() == true
listView.selectionModel()->selectedIndexes().count() == 0
listView.selectionModel()->currentIndex().isValue == false
The same thing happens when using select instead of setCurrentIndex. When using the following line...
listView.selectionModel()->select(
model.index( 3, 0, QModelIndex() ),
QItemSelectionModel::SelectCurrent );
... you get the following:
listView.selectionModel()->hasSelection() == true
listView.selectionModel()->selectedIndexes().count() == 1
listView.selectionModel()->currentIndex().isValue == false
And after resetting the model it looks like this:
listView.selectionModel()->hasSelection() == true
listView.selectionModel()->selectedIndexes().count() == 0
listView.selectionModel()->currentIndex().isValue == false
The TaskTracker entry says that the bug is fixed, so this is likely to have been fixed in the Qt 4.3 snapshots. If you're using an earlier release it looks as if the selectedIndexes().count() is a better way to determine if something is selected.
0 Comments:
Post a Comment
<< Home