How to change row or cell color in TableView with SqlQueryModel
-
Hi,
I have SqlQueryModel and read rows from query. I load this on TableView and want to change some rows or cells in TableView.?
There are many sample about Item Model but I couldn't see one about SqlQueryModel.
My code is below:QSqlQueryModel *modelTableIhtiyac; this->modelTableIhtiyac = new QSqlQueryModel(this); // ,db modelTableIhtiyac->setQuery("select stok_kod, stok_ad, stok_tip, stok_miktar, stok_birim, birim_fiyat, toplam_fiyat, birim_agirlik, toplam_agirlik FROM uretim_listesi_2 WHERE gorunsun=1 ORDER BY stok_tip, stok_kod;",baglanti::mdb2()); //TvDetay is TableView... ui->tVDetay->setModel(modelTableIhtiyac);
How can I change some rows or cells in above code?...
Regards,
Mucip:) -
Hi,
QSqlQueryModel is read-only. Why not use QSqlTableModel ?
-
And to add to @JonB, if it's cosmetic change that won't alter the database content, QIdentityProxyModel is likely what your looking for.
-
That's another possibility yes. One thing to take into account is that if these customisation were to be "view specific", changing the base model would "propagate" them to all views showing the model data. The QIdentityProxyModel allows for more fine grained control in that regard.
-
What kind of sample are you looking for ?
-
Right in the documentation for the class that I already linked to earlier.
-
You can use the
RoleMaskProxyModel
of this library.
I'm working on merging this into Qt 5.13: https://codereview.qt-project.org/245572 (at the time of writing it's marked as draft so private) -
@Mucip said in How to change row or cell color in TableView with SqlQueryModel:
Hi @JonB,
Yes. Just want to show data and change color of the desired rows.Regards,
Mucip:)Sorry, I have been away for a few days. As I wrote earlier, if all you want to do is set background color of certain rows you could skip proxy models and just override
QSqlQueryModel::data()
to deal withQt::BackgroundRole
. It depends whether you can figure the color you want for whatever rows very simply in code, or whether you need something more sophisticated which a proxy model could provide.