Usage of Wt::WAbstractItemModel::match(...)

Added by Fabian V 12 days ago

Hi, I'm trying to search an item in a model. For example, I've created a model with some numbers in the first column and now I'd like to find the right row/item where number "9" is set.
My problem is, I don't know how to use the Wt::WModelIndex-argument.

  Wt::WModelIndex idx;
  auto hits = sut->getModel()->match(idx, Wt::ItemDataRole::Display, "9", -1, Wt::MatchFlag::Wrap);

So I would expect that:

  REQUIRE(hits.size() == 1);

...but it's zero.

I guess I'm using Wt::WModelIndex wrong so it would be great if someone can tell me how to initialize it.

Thanks!


Replies (3)

RE: Usage of Wt::WAbstractItemModel::match(...) - Added by Roel Standaert 11 days ago

With that line:

Wt::WModelIndex idx;

you're just creating an invalid index.

You can get a valid model index to the first cell in column n with:

Wt::WModelIndex idx = sut->getModel->index(0, n);

RE: Usage of Wt::WAbstractItemModel::match(...) - Added by Fabian V 11 days ago

Ok thank you - that's better. Unfortunately the result is still not what I expect.

When I set the flags this way:

  auto hits = sut->getModel()->match(sut->getModel()->index(0, 0), Wt::ItemDataRole::Display, std::any("9"), -1, Wt::MatchFlag::StartsWith | Wt::MatchFlag::Wrap);

...the number of results is the same as the row-count in the model. Although the other entries are completely different.

When I set the flags this way:

  auto hits = sut->getModel()->match(sut->getModel()->index(0, 0), Wt::ItemDataRole::Display, std::any("9"), -1, Wt::MatchFlag::StringExactly);

...the number of results is zero.

So it looks like I'm still doing something wrong.

The goal is to get the item or row-number where "9" is written in.

RE: Usage of Wt::WAbstractItemModel::match(...) - Added by Fabian V 11 days ago

[SOLVED]

Ok, looks like this works for me:

  auto hits = sut->getModel()->match(sut->getModel()->index(0, 0), Wt::ItemDataRole::Display, 9);

And I can change an item in the same row by:

  sut->getModel()->item(hits[0].row(), 2)->setText("22");

(1-3/3)