Unsolved How to change the color of the row in the Sqlite table?
-
I insert the data in the Sqlite table from my code, I see the table from SqliteBrowser on Windows, not in the code, I want to change the color of a row in the table? How can I do it?
db = QSqlDatabase::addDatabase( "QSQLITE" ); db.setDatabaseName( Dbname ); if( !db.open() ) { qDebug() << db.lastError(); qFatal( "Failed to connect." ); } else qDebug( "Connected to database!" ); QSqlQuery qry(db); qry.prepare( "SELECT * FROM "+mths+""); if( !qry.exec() ) qDebug() << qry.lastError(); else { if(!SearchFlag) { qry.prepare( "INSERT INTO "+mths+" ( WeekDay, Date,TimeIn, TimeOut ) VALUES (:wDay,:date,:timeIn,:timeOut)" ); qry.bindValue(":wDay", wDay); qry.bindValue(":date", date); qry.bindValue(":timeIn", timeIn); rowcount++; if( !qry.exec() ) qDebug() << qry.lastError(); else qDebug( "Inserted!" ); } db.close(); } db.close(); }
-
Hi
The color you see in SqliteBrowser is not stored in
the database and cannot be changed.
i assume you mean the white / grey / white / grey way of showing the rows.Else please explain what color you mean as rows dont have colors as such.
-
@mrjj Hi, I want to record absenteeism in the Sqlite database
And I want to set red/black/gray/... (just for difference) rows in table for holiday -
@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!