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

Florian Bruhin me at the-compiler.org
Wed Feb 19 13:54:43 CET 2020


On Tue, Feb 18, 2020 at 04:49:57PM -0500, Matthew Woehlke 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)

I'd recommend running QAbstractItemModelTester (or ideally, including it in
your testsuite) over custom models:

https://doc.qt.io/qt-5/qabstractitemmodeltester.html

It'd likely have caught an issue like this (though I haven't checked).

Florian

-- 
me at the-compiler.org (Mail/XMPP) | https://www.qutebrowser.org 
       https://bruhin.software/ | https://github.com/sponsors/The-Compiler/
       GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
             I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200219/0da63296/attachment.sig>


More information about the Interest mailing list