Luca Fülbier
Luca Fülbier

Reputation: 2731

Qt setCurrentIndex does not emit currentChanged for invalid ModelIndex

I have a wizard where the user is only allowed to advance when he has selected an entry from a QListView. To check that, i have connected my method containing the validation logic to the currentChanged signal, but the signal is not emitted if the selected Index is invalid:

// It's connected like this
connect(connectionsListView->selectionModel(), 
        SIGNAL(currentChanged(QModelIndex,QModelIndex)),
        this, SLOT(connectionSelected(QModelIndex)));

// Does not emit for empty ListModel
connectionsListView->selectionModel()->setCurrentIndex
    (connectionListModel->index(0, 0), QItemSelectionModel::SelectCurrent);

// Does not emit whatsoever
connectionsListView->selectionModel()->clear();

// Valid selections are delegated to my validation logic and handled correctly

Any ideas why this happens and what i can do to fix that? Currently i have a second signal that i emit in my code on occasions where the index could be invalid, but i would prefer Qt simply emitting the signal if the index is invalid.

Upvotes: 0

Views: 1325

Answers (1)

The first line can't emit because the index didn't change: it was invalid, and still is invalid, since the model is empty. There is an invariant: on an empty model, the selection index is invalid. Thus for as long as the model is empty, you should never expect the currentChanged signal to be emitted.

The second line doesn't emit for the same reason: the index still is invalid.

You might need to create a test case that reproduces the issue: you need to do something that changes the value returned by currentIndex yet doesn't fire the signal. Note that resetting the selection model will not fire the signal. You could treat as a bug perhaps.

Upvotes: 2

Related Questions