[Qt-interest] custom model question, cells depending on other cell
franki
franki at franki.eu.org
Sun Dec 20 12:11:52 CET 2009
Friday 18 of December 2009 17:43:26 Andre Somers napisał(a):
> franki wrote:
> > Friday 18 of December 2009 08:18:02 André Somers napisał(a):
> >> Hi,
> >
> > Hi,
> >
> > Thank you for answer,
> > My data comes from sql, I've used QSqlQueryModel, to be honest, earlier I
> > always did such an application in php, so maybe I'm looking for similar
> > solution, because example with background color of the table is just
> > small one. I've used to fetch all data from sql query, and then (if
> > necessary) made calculation by iterating throught all data, and changing
> > what should be change, and then passing entire array to template engine
> > to display it. That's very flexible, and allow me to change everything I
> > need in the data fetched from sql, and that's what i'm looking for.
> > The second solution You wrote, would probably solve my background color
> > problem, but 5 minuts later I would have another one (like adding
> > additional column probably or something else), and I feel that best
> > solution would be to modify (somehow) data received from sql.
>
> If that is what you are looking for, than I suggest that that is what
> you do!
> As long as the amount of data is reasonable and you need it fetch it all
> anyway, I don't see a real problem in downloading all the data into
> memory, do stuff with it, and then display it. I do that too when
> needed. So: go ahead, create a handy data structure to store all the
> data you need, do any calculation & modification you need, and create
> your own QAbstractItemModel derived* model based on that data structure
> that handles stuff like coloring.
> In this scenario, I'd ditch your use of QSqlQueryModel though, and use
> QSqlQuery directly to get the data you need.
Thanks for clarification, I'll try this one for the sake of practice.
Programming in C is a bit different than script language like php, so I
haven't been dealing with structures so far.
>
> Another possible solution for the calculations and modifications: can't
> you do them efficiently in SQL itself? It really depends on what you
> want to do of course, but sometimes good old SQL is not that bad...
I do some simple CAST or joins if needed, but sometimes it's not enough.
>
> * Replace QAbstractItemModel by one of the (abstract) models based on it
> if they are more convenient.
>
> > I haven't read very much about proxy models, will they allow me to
> > iterate throught data and calculate data and change values in role?
>
> Proxies are used mainly for filtering, but they can be used for other
> things like modifying data. Note that a proxy model is also a
> QAbstractItemModel subclass... That is: nothing prevents you to
> re-implement the data() method and return whatever you like there. You
> can even change the data structure (flatten trees for instance) if you
> want, but that kind of thing is non-trivial to say the least.
> So sure, if you want to change values in a proxy model, you can. But,
> I'd be cautious with this. Changing the colour role for a row based on
> the value of one of it's columns and then hiding that column is one
> thing, but iterating over the whole dataset to calculate some value is
> quite another IMHO. Still, if you would be able to cache critical data
> for such calculations, it may be a viable solution.
>
> For instance, say you have a table with numbers of items in each row,
> and you'd like to add a column that displays the proportion of the total
> numbers of items in the set. You'd only have to calculate the total once
> (assuming the data behind it not too volitile), so this could be done
> quite easily in a proxy model. You could have the proxy model get the
> data by iterating, or you could supply the proxy with the sum yourself
> based on a perhaps more efficient SQL query with a SUM () statement in
> it. However, I'd only use a proxy model solution if I'd be reasonably
> sure I can use the created solution again on a different source model,
> either in the same or in a different application. That is: it has to be
> reasonably generic. Otherwise it's not worth the trouble, IMHO.
>
> > Second solution seems to be easy, column order is always the same,
> > besides I can search for data using QSqlRecord::indexOf but then data in
> > model remains unchanged, and customModel::data() will have to calculate
> > roles and data each time view request the data.
> >
> > So if I want to change values that comes from sql, inside model, and then
> > when I connect a view, it gets always previously calculated data from
> > model, should I opt for solution 3 - proxy model?
>
> I'm sorry, I don't get what you're trying to say here. Is the data in
> your database volitile? That is: does it change (frequently) while your
> application is running, by other means than your application itself?
>
> > And is it ever possible (in read-only model) to change values fetched
> > from sql, values like DisplayRole, and others, and then let the view to
> > operate on modified data without implementing proxy model, or maybe those
> > data in model are read-only?
>
> The read-onlyness has only to do with if changes in the data can be
> written back, not if the data you pass along for the view can be
> modified in the process. Do you plan to write back data to your database
> from your model?
My data isn't volatile, it is changed only by my application,
and I don't plan to write back data to database from this model.
So I'm still unsure, Is it possible to change data in model QSqlQueryModel
fetched from sql? Or not?
Because when I was trying to use a few times setData function it always
returns FAILED, and in documentation about setData there is: "The base class
implementation returns false."
Even if I subclassed QSqlQueryModel inside my own setData function I think I
still have to use QSqlQueryModel::setData to change my data from sql, right?
But maybe there is something else that cause that setData returns FAILED in my
app.?
Best regards
Marek
>
> André
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list