Following a simple example, I'm trying to make a custom subclass of QSqlQueryModel. I see from that example how I can, e.g., color text in a specific column, but what I can't figure out is how to color text depending on a value from a different item in the same row. Suppose, the query returns two columns, column1 and column2, both holding text entries. So I just want that the text in column1 is colored (e.g., red) when the corresponding text in column2 contains some substring. The following example compiles, but produces no effect:
customsqlmodel.h:
class CustomSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
CustomSqlModel(QObject *parent = 0);
QVariant data(const QModelIndex &item, int role) const Q_DECL_OVERRIDE;
};
In customsqlmodel.cpp I have:
QVariant CustomSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (role == Qt::TextColorRole && record().field(1).value().toString().contains(QString("substring")) )
return QVariant::fromValue(QColor(Qt::red));
return value;
}
By the way, do I understand it right that the QSqlQueryModel::data() is called whenever the related QTableView tries to render the data on the screen?
Another thing I cannot get is how to pass from the model to the view only some of the columns. Of course, I can use setHiddenColumns for QTableView, but it is only a workaround. Having a QSqlQUeryModel which return, say, five columns, is it possible to bind to QTableView only first three of those columns?
Aucun commentaire:
Enregistrer un commentaire