[Qt-interest] custom model question, cells depending on other cell
franki
franki at franki.eu.org
Fri Dec 18 17:06:38 CET 2009
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.
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?
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?
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?
Greetings
Marek
>
> I see several solutions to your problem, but it also depends a bit on if
> you know what cell to look at, and if that is always the same. The general
> options are:
>
> 1) Since your column d won't be shown, why is it in your model? Your model
> should be based on some underlying data structure. Just set the value
> there, and in your customModel::data(), simply return the right color
> depending on the value in that underlying data structure.
>
> 2) Do it the way you do now, but simply in your custom model, if the data
> for the color role is requested, request the value role of the right column
> yourself, evaluate that, and return the right color.
>
> 3) Don't modify the model supplying the original data, but use a proxy
> model instead that basically does the same. Instead of passing on column d,
> use it to override the returned color for the other columns.
>
> 4) Create a delegate that does this. Also in a delegate, you can request
> data for other cells than the one the delegate is called for.
>
>
> You see: plenty of roads to choose from. The order above does not suggest
> an order in what is the best solution in your case.
> Since your data seems to come from an SQL model, I guess I would go for
> option 3. It is more easily applicable to other models in the future than
> options 1 and 2. Option 4 is also possible, but it would not automatically
> hide the column d in one go. Option 3 would in principle allow you to
> implement some caching, where that would be a bit awkward in option 4 (but
> not impossible, I've done it before). If that is possible and useful
> depends on how volatile your data is and how expensive access to it is I
> guess.
>
> André
>
>
> -----Oorspronkelijk bericht-----
> Van: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] Namens franki
> Verzonden: donderdag 17 december 2009 21:09
> Aan: qt-interest at trolltech.com
> Onderwerp: [Qt-interest] custom model question, cells depending on other
> cell
>
> Hi,
>
> I cant figure out how to change background color of the entire row in
> QTableView depending on the value of the one of the cells, for example:
> select a,b,c,d from my_table;
> column "d" is never shown in QTableView, but if it has value=1 background
> color for this row should be, lets say blue.
> According to documentation, I should made custom model, so this is what I
> did:
> Constructor:
>
> custSearchModel::custSearchModel(QObject *parent): QSqlQueryModel(parent)
> {
> }
>
> data function:
> QVariant custSearchModel::data(const QModelIndex &index, int role) const
> {
> bool ok=false;
> QVariant value = QSqlQueryModel::data(index, role);
> if(index.column()==0 && index.row()==0) {
> qDebug() << "object role:"+QVariant(role).toString()+"
> object
> value:"+value.toString();
> }
> }
>
> As I noticed view asks several times about each object, fetching different
> roles, so it's not a problem to return what I want for that object. But
> when
>
> view fetches cell "a" how should I know what to return if that depends on
> cell "d" ?
> Should I have some other array in memory to remember all row numbers where
> cell "d" has value=1 ?
> I thought also, that if I have custom model, I will be able to walk
> throught
>
> the entire model and by using setData, i will set what i want for each
> role,
>
> but now when I use setData, it always returns FALSE.
> For example (from other function outside custSearchModel class)
>
> searchCustModel=new custSearchModel(this);
> // some code to set model query, view, etc.
> QModelIndex index=searchCustModel->index(0,0,QModelIndex());
> if(searchCustModel->setData(index,qVariantFromValue(QColor(Qt::blue)),
> Qt::BackgroundColorRole))
> qDebug() << "setData ok";
> else
> qDebug() << "setData failed";
>
> Please help, how it should be done?
>
> Thanks
> Marek
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
> _______________________________________________
> 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