Updating tables after using QSqlRelationalTableModel:setFilter()

  • I have a subclassed QSqlRelationalTableModel and I'm trying to update the database after applying a filter to hide empty rows. However, the queries I use inside my model no longer work correctly. It seems that, after the filter is applied, a temporary model is created with less rows. No matter what I tried, I can't make this work. The filter is applied in another class when I create the view.
    The query copies all the text from a column in the referenced table to a column in the reference table. If I remove the filter, everything works perfectly. Here is a version of the function with the query:

    void ProjectModel::copyAll() {
       this->setFilter(QString()); // Here I'm trying to reset the filter
       QSqlQuery query;
       query.prepare(QString("UPDATE %1 SET target = :text "
                           "WHERE sourceID = :id")
       for (auto i = 0; i < rowCount(); ++i) {
           QModelIndex sourceIndex = index(i,MyModel::SIdReference);
           QModelIndex targetIndex = index(i,MyModel::TText);
           if (sourceIndex.isValid()) {
               if (targetIndex.data().isNull()) {
                   query.bindValue(":id",targetIndex.row()+1); // +1 = rowid in db
                   if (!query.exec()) {
                       qDebug() << "Not updated: " << query.lastError().text();
       this->setFilter("sourceText != ''"); // Here I try to reapply the filter
       if (select()) {

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.