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. QStandardItemModel and QSortFilterProxyModel: maintaining correspondence between indexes and items (and underlying data)
Qt 6.11 is out! See what's new in the release blog

QStandardItemModel and QSortFilterProxyModel: maintaining correspondence between indexes and items (and underlying data)

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I have big problems using Qt model/view architecture. Looks like I'm misunderstanding something.

    I have a vector of data that I want to visualize with QTreeView. I use QStandardModel, like this:

    @void fillFromList(const std::vector<CFileSystemObject> &items)
    {
    _model->clear(); // QStandardItemModel
    _model->setColumnCount(NumberOfColumns);

    for (int i = 0; i < (int)items.size(); ++i)
    {
    QStandardItem * fileNameItem = new QStandardItem();
    fileNameItem->setData(items[i].name(), Qt::DisplayRole);
    fileNameItem->setData(items[i].hash(), Qt::UserRole); // Unique identifier for this object;
    _model->setItem(i, NameColumn, fileNameItem);

                                     //... and so on for all other columns ...
    

    }
    }@

    Then, on top of this, I have a model inherited from QSortFilterProxyModel with overriden lessThan for custom sorting. But I have problems getting original model's items from indexes:

    @bool CSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
    {
    assert(left.column() == right.column());
    assert(left.isValid() && right.isValid());
    const int sortColumn = left.column();

    QStandardItemModel * srcModel = dynamic_cast<QStandardItemModel*>(sourceModel()); // Ugly!
    QStandardItem * l = srcModel->item(left.row(), left.column()); // Is sometimes null!
    QStandardItem * r = srcModel->item(right.row(), right.column()); // Has been OK so far

    if (!l || !r)
    return true;
    // Actual sorting done below...
    }@

    Is there a mistake in my code? Why can srcModel->item yield null for one of the items passed to lessThan? The idea here is to retirive the original QStandardItem, then hash from its data(Qt::UserRole) and then the actual underlying data from the array that's being visualized. Is the general design OK? Any improvement suggestions? But mainly I'm looking to fix the severe bugs at this point.

    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