Frage

I am displaying a QSqlTableModel in QSqlTableModel::OnManualSubmit mode.

I want to run SQL style queries on my local copy, for example I have a getMax function that changes as I add new rows. If my user locally changes the QSqlTableModel during interaction I expect the getMax to produce a different input, but instead the getMax function remotely queries the data and the result will always be the same independent of any changes the user has made.

int TestMe::getMax(QString col,QString table)
{
    QSqlQuery query;
    sf(query.exec("SELECT MAX("+col+") FROM "+table));
    sf(query.first());
    auto rec = query.record();
    return rec.field(0).value().toInt();
}

How do I run queries on my local copy of the model?

War es hilfreich?

Lösung

QSqlTableModel (or more generally QSqlQueryModel) delegates query execution to QSqlQuery. When results from query are received they are stored internally in form of a table with rows and columns and you can access them using QModelIndex and QAbstractItemModel::data().

QSqlTableModel itself is not SQL interpreter. Not at all. It doesn't provide any analysis routines for you. It isn't the purpose of this class. If you need analysis of data in the model, you have to write it yourself.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top