PDA

View Full Version : Visualising data from mysql table



vrltwe
28th December 2013, 17:49
For the purpose of visualizing data from a MySQL table, a QTableView is being used in conjunction with a QSqlTableModel. As the intention is not to edit the values then the QTableView is configured with:
ui->tbViewTmHistTk->setEditTriggers(QAbstractItemView::NoEditTriggers) ; Is this a good approach in order to prevent editing the data?

ChrisW67
30th December 2013, 05:50
Yes, but the model remains editable in any view that allows editing. QSqlQueryModel gives you a truly read-only model.

vrltwe
30th December 2013, 13:49
Ok, I understand.

I think QSqlTableModel was used because it enables changing the color of items, re-writing the data function.
QVariant SqlTableModelTk::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if ( role == Qt::TextColorRole )
if( (index.column(), index.row()) )
return QVariant::fromValue(QColor(Qt::red));
return value;
}
As it doesn't exist a data function for QSqlQueryModel then I suppose I'm in the correct direction. Is there another approach which could be applied in order to attend both requirements, change item color and prevent editing the database?

Thanks for the attention.

anda_skoa
30th December 2013, 18:14
QSqlQueryModel is a subclass of QAbstractItemModel, so it has a data() method.

Cheers,
_