[Interest] Why doesn't my model filter update?

Sze Howe Koh szehowe.koh at gmail.com
Wed Feb 19 00:38:48 CET 2020


On Wed, 19 Feb 2020 at 05:51, Matthew Woehlke <mwoehlke.floss at gmail.com> wrote:
>
> On 18/02/2020 16.31, Konstantin Shegunov wrote:
> > On Tue, Feb 18, 2020 at 11:20 PM Matthew Woehlke wrote:
> >> I wonder if anyone else can spot it? ;-)
> >
> > Without knowing anything about the code at all, my best guess based on a
> > very quick glance would be the range for the dataChanged is wrong.
>
> Okay, I'll give you half a cookie, since I can't really say much more
> without giving it away. (Incidentally, if I hadn't already figured it
> out, you comment probably would have helped!)
>
> Here's the broken code:
>
>   if (auto const rows = this->rowCount())
>   {
>     auto const& first = this->index(0, 0);
>     auto const& last = this->index(rows, 0);
>
>     emit this->dataChanged(first, last, {MyFilterRole});
>   }
>
> It fell victim to one of the two hard problems of programming (as
> enumerated by Leon Bambrick¹). To wit, `last` is an invalid index, which
> trips one of the sanity checks in QSortFilterProxyModel's internal logic.
>
> If you can't tell *why* `last` is invalid, well, keep looking until you
> can ;-). All the information you need to spot the problem is in the
> above snippet, and it's *obvious* once you see it. (Note: assume that
> the class otherwise behaves in a correct fashion.)
>
>https://www.goodreads.com/quotes/7443069)
>
> --
> Matthew

The last valid row index is (rows-1), not (rows).

Do I get the other half-cookie?


Regards,
Sze-Howe


More information about the Interest mailing list