QTableView from model
Maurizio Berti
maurizio.berti at gmail.com
Mon Sep 11 04:34:54 BST 2023
Adding further insights to what Baz correctly pointed out.
First of all, models should know **nothing** about the views connected to
them. That's not their responsibility and their behavior should not change
depending on it (do some research about encapsulation and separation of
concerns). That's important, since models could be shown in multiple and
very different views (or not even shown at all): they should work
transparently and consistently, in any case.
So, in principle, there is no direct nor immediate way for a model to know
the view(s) it's been shown, because there shouldn't.
If the model needs to know the views that are connected to it in order to
work properly, 99% of the time it's because important concepts behind the
program are poorly implemented (simply put: you're doing the wrong thing
for the wrong reasons).
If different views have to respond differently to what the same model
returns, then you probably need proper item delegates, as that's their
purpose. *Then* you could eventually implement some custom functions in the
model that may return specific data based on the circumstances, but it's
still a matter of responsibilities that the model shouldn't immediately be
aware of, at least in its basic implementation.
Still, there may be some fringe situations: I cannot think of reasonable
ones right now, but I'm thinking about the parallel of graphics scenes (and
their items), which sometimes *do* need to know the paint device (or the
view) they're being shown.
If you really need that, then just implement it in your views and models:
override `setModel()` (or implement a related function) for the view and
add related support in a model subclass, so that it keeps track of the
views it's been set. For safety reasons, you should also ensure that you
connect the view's destroyed signal in order to remove any reference for
deleted views.
But, as said, that shouldn't be necessary, and if you think that you need
it then think twice, because you're probably doing it for the wrong reason.
Regards,
MaurizioB
Il giorno dom 10 set 2023 alle ore 16:59 Glenn Holmer <cenbe at protonmail.com>
ha scritto:
> Is there any way to get a reference to a table model's table (from the
> model)?
>
> --
> Glenn Holmer (Linux registered user #16682)
> "After the vintage season came the aftermath -- and Cenbe."
>
>
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230911/d869a9a9/attachment-0001.htm>
More information about the PyQt
mailing list