
Originally Posted by
emorgen
Strange, I checked the isReadOnly and it said false.
One of the nice things about Qt is that sometimes you can figure something out by just opening up the source code and taking a look. Here’s the code for QSqlQueryModel::record(int):
{
if (row < 0)
return d->rec;
for (int i = 0; i < rec.count(); ++i)
rec.setValue(i, data(createIndex(row, i), Qt::EditRole));
return rec;
}
QSqlRecord QSqlQueryModel::record(int row) const
{
Q_D(const QSqlQueryModel);
if (row < 0)
return d->rec;
QSqlRecord rec = d->rec;
for (int i = 0; i < rec.count(); ++i)
rec.setValue(i, data(createIndex(row, i), Qt::EditRole));
return rec;
}
To copy to clipboard, switch view to plain text mode
Without looking very far, we can see that all the QSqlFields in the returned QSqlRecord are set using QSqlRecord::setValue just before the record is returned; and the code for that function is:
void QSqlRecord::setValue(int index,
const QVariant
& val
) {
if (!d->contains(index))
return;
detach();
d->fields[index].setValue(val);
}
void QSqlRecord::setValue(int index, const QVariant& val)
{
if (!d->contains(index))
return;
detach();
d->fields[index].setValue(val);
}
To copy to clipboard, switch view to plain text mode
where d->fields is a QVector<QSqlField>.
Since QSqlField::setValue is defined to do nothing when QSqlField::isReadOnly returns true, we can conclude that isReadOnly for a field in a record from a QSqlQueryModel returns no useful information: it must always have been set to false.

Originally Posted by
emorgen
I guess the copy is editable so it edits the copy but will not alter the original.
Only the fields in the QSqlRecord returned to you are writable; changing them will not affect the model (neither the database nor what is seen in an attached view); and QSqlQueryModel::setData will not work, because it has not been re-implemented from QAbstractItemModel::setData.

Originally Posted by
emorgen
I originally was using a QSqlTableModel but I went to a query because I want to add restrictions to the table. I was afraid it will still attempt to read the database before the filter which could be costly.
According to the documentation at QSqlTableModel::setFilter, that function sets up a WHERE clause; and as long as you call it before you call QSqlTableModel::select(), you won’t query the database except using the WHERE clause.
Bookmarks