Unsolved How to change the color of the row in the Sqlite table?
-
@isan
Hi
That is actually done by the view/widget where you show the data.
You can do it likevoid setRowBackground(const QBrush& brush, QAbstractItemModel* model, int row, const QModelIndex& parent = QModelIndex() ) { if(!model || row < 0) { return; } if(row >= model->rowCount(parent)) { return; } if(parent.isValid()) { if(parent.model() != model) { return; } } for(int i = 0; i < model->columnCount(parent); ++i) { model->setData(model->index(row, i, parent), brush, Qt::BackgroundRole); } }
credits to @VRonin from
https://forum.qt.io/topic/75151/qtablewidget-set-specific-row-s-color/5 -
@mrjj That method only works if the model supports multiple roles (i.e. QSql*Models are a no go). I still didn't understand where OP wants the color to appear. Is it in a custom Qt application or in a completely unrelated "SqliteBrowser on Windows"?
-
@VRonin
Hi.
thank you for clarification.
Also unclear to me, however since he shows
code with QSqlQuery , i assume he will have a view/widget to show the data in. -
@mrjj yes I use tableView to show QSqlite Table
after thisui->tableView->setModel(model);
I should use setRowBackground function?
and what are the inputs of function? -
@isan
hi
its actually the Qt::BackgroundRole
that is key.
the setRowBackground is just a helper function VRonin wrote.- and what are the input function?
Im not sure what you mean by this ?
However, if you mean save back to the database, that's not really possible.
Unless you change the table definition to include a QColor or something like that.
- and what are the input function?
-
@mrjj my mean : what are QAbstractItemModel *model and const QModelIndex &parent
I do this and row color doesn't make changeQSqlQueryModel *model =new QSqlQueryModel(); db1.setDatabaseName( DBn ); if( !db1.open() ) { qDebug() << db1.lastError(); qDebug( "Failed to connect." ); } else qDebug( "Connected to database!" ); QSqlQuery qry(db1); qry.prepare( "SELECT * FROM _"+Ms+""); qry.exec(); model->setQuery(qry); ui->tableView->setModel(model); setRowBackground(QColor(250,0,0),ui->tableView->model(),1);
-
@isan said in How to change the color of the row in the Sqlite table?:
setRowBackground(QColor(250,0,0),ui->tableView->model(),1);
@VRonin said in How to change the color of the row in the Sqlite table?:
That method only works if the model supports multiple roles (i.e. QSql*Models are a no go).
-
@VRonin said in How to change the color of the row in the Sqlite table?:
That method only works if the model supports multiple roles (i.e. QSql*Models are a no go).
my model something like :
That method not work for this?
For example I want to set color for rows that have Thursday and Friday -
Check out this library: https://github.com/VSRonin/QtModelUtilities
In particular this example: https://github.com/VSRonin/QtModelUtilities/blob/1.0.0/examples/exam_RoleMaskProxyModel/exam_rolemaskhighlight.cpp -
This post is deleted!