Nominate our 2022 Qt Champions!

QAbstractItemView::setSelectionModel failing

  • Hi Everyone!
    I'm new to the model/view system, and facing an error I don't know how to get past. So all I'm trying to do is set a selection model in a QListView. Here's a really simple example of what I'm doing, and this produces the error for me.

    Here's the header:

        QItemSelectionModel* testModel;
        QListView* testList;

    And the source:

        testModel = new QItemSelectionModel();
        testList = new QListView(this);

    And I'm getting a runtime error:
    QAbstractItemView::setSelectionModel() failed: Trying to set a selection model, which works on a different model than the view

    So first off, I think this error is worded confusingly; I found it in the source code, but I'm still not sure what it's comparing to:

    void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
        // ### if the given model is null, we should use the original selection model
        if (selectionModel->model() != d->model) {
            qWarning("QAbstractItemView::setSelectionModel() failed: "
                     "Trying to set a selection model, which works on "
                     "a different model than the view.");
        if (d->selectionModel) {
            disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                       this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
            disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
                       this, SLOT(currentChanged(QModelIndex,QModelIndex)));
        d->selectionModel = selectionModel;
        if (d->selectionModel) {
            connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                    this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
            connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
                    this, SLOT(currentChanged(QModelIndex,QModelIndex)));

    Most of the stuff online is people wrestling with using the same model in multiple views, which I am not trying to do. I'm not doing anything else with the model or view right now. (but I'm definitely doing something wrong haha)
    Any ideas?

  • Lifetime Qt Champion


    Unless you implement a custom selection model there's no need for you to create one like that

    Just use your view, set a model on it and you will be good to go.

    As it is, your view as no model so the selection model can't even work.

  • You might have to spell this out for me. :)
    Are you saying you just create a model inline when I call setSelectionModel?
    I was using a QStringListModel before, and using setModel was working fine for that, but moving to selection models has gone poorly.

    I am implementing a custom selection model, which is when I ran into this error - I'm just working backwards to see where I went wrong; I was worried my subclass was the problem.

  • Lifetime Qt Champion

    After the view creation:

    1. create your model
    2. set the model on the view
    3. create the custom selection model passing the model as constructor parameter or calling setModel
    4. set the selection model on the view

    The order is important as when you set the model on the view, a new selection model will be created for the view.

  • Ok - I'll work on this and post my code when I get it working. Thanks!

  • Alright I had some stuff commented out that took me forever to debug, but I got it!

    So the piece I was missing is that a View has both a Model and a SelectionModel. Obviously this is listed in the documentation, but I missed it. Here's the chunk of documentation regarding that:

    Handling selections of items
    The mechanism for handling selections of items within views is provided by the QItemSelectionModel class. All of the standard views construct their own selection models by default, and interact with them in the normal way. The selection model being used by a view can be obtained through the selectionModel() function, and a replacement selection model can be specified with setSelectionModel(). The ability to control the selection model used by a view is useful when we want to provide multiple consistent views onto the same model data.

    Anyway, so it turns out all I needed was a subclass of QStringListModel to handle my data that I had already made, and I got lost in the sauce trying to turn that into a QItemSelectionModel, thinking it was a direct replacement for a Model. I guess I feel like it could be more clear that a View has both a Model and a SelectionModel attached as soon as you give it a Model, but it's there in the docs so I guess it's on me. :)

    Thanks for the help!

Log in to reply