Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not grocking listview selection
Forum Updated to NodeBB v4.3 + New Features

Not grocking listview selection

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Spaced Cowboy
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Spaced Cowboy
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mohsen
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Spaced Cowboy
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mohsen
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved