Not grocking listview selection



  • I've got some code that wants to add items to a QStringList-backed QListView, and select some of them as it goes. I can't for the life of me get the selection to happen...

    Here's the setup of the QListView:(_sensorsList is a QStringList)

    @ _sensors = new QListView(this);
    _sensors->setModel(new QStringListModel(_sensorsList));
    _sensors->setSelectionMode(QAbstractItemView::MultiSelection);
    _sensors->setSelectionBehavior(QAbstractItemView::SelectRows);

    model = _sensors->selectionModel();
    connect(model, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
    this, SLOT(sensorSelectionChanged(QItemSelection,QItemSelection)));
    @

    .. and on launch, the following method is called:

    @void GraphWidget::repopulateSensorsList(void)
    {
    Database *db = Database::sharedInstance();
    QString *group = selectedGroupName();

    if (group != NULL)
    {
    _sensors->clearSelection();
    _sensorsList.clear();

    QList<Sensor *> allSensors = db->sensorsList();
    QList<Sensor *> groupSensors = db->sensorsForGroup(*group);
    QItemSelectionModel *selectModel = _sensors->selectionModel();
    QStringListModel model = (QStringListModel)_sensors->model();

    for (int i=0; i<allSensors.size(); i++)
    {
    Sensor *current = allSensors.at(i);
    bool foundMatch = FALSE;

    for (int j=0; j<groupSensors.size() && !foundMatch; j++)
    {
    Sensor *match = groupSensors.at(j);
    if (current->equals(match))
    foundMatch = TRUE;
    }

    QString displayName;
    QTextStream(&displayName) << current->node()
    << " : "
    << current->name();

    _sensorsList.append(displayName);
    if (foundMatch)
    {
    fprintf(stderr, "Setting index %d\n", i);
    QModelIndex idx = model->index(i);
    selectModel->select(idx, QItemSelectionModel::SelectCurrent);
    }
    }

    model->setStringList(_sensorsList);
    _sensors->setSelectionModel(selectModel);
    }
    }
    @
    ... I can see the code for the selection is being called (the fprintf is happening), but the widget itself never actually selects anything. Help!

    Cheers
    Simon



  • Ok, replying to myself, but after struggling for a while I managed to get it to work by dividing the code up into two methods - the first populates the stringlist, the second does the selection. Perhaps I was trying to do too much in one go...

    Simon



  • you should have a QItemSelectionModel object as a pointer. then your matched list indexes should be added to this model while your "for" is going.
    after that searching is completed, use _sensors->setSelectionModel([QItemSelectionMode Object]);

    also this is ExtendedSelection and not MultiSelection



  • [quote author="mohsen" date="1294986427"]you should have a QItemSelectionModel object as a pointer. then your matched list indexes should be added to this model while your "for" is going.
    after that searching is completed, use _sensors->setSelectionModel([QItemSelectionMode Object]);

    also this is ExtendedSelection and not MultiSelection[/quote]

    But that's exactly what the code above does - 'selectModel' is my QItemSelectionModel*; the if(foundMatch) block sets the index into the model, when a match is found; finally I call setSelectionModel(selectModel);

    The code that works for me is actually slightly different. I looked in the source for QListView and they're using a QItemSelection object to accrue selections. I tried it that way, after separating into two methods (one sets the items into the list, the second selects a subset of the items) and it works like a charm.

    I still think a [ListView]->selectItemAtRow(int row, bool select=true) would be a nice addition to the QListView class - this is unreasonably hard for such a simple task.

    Simon



  • selectModel->select(idx, QItemSelectionModel::SelectCurrent);
    is in your "for"! you should move outside! then you have to use something like a->selectedRows(i).append(i) when (foundmatch) is true


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.