Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating tables after using QSqlRelationalTableModel:setFilter()
Forum Updated to NodeBB v4.3 + New Features

Updating tables after using QSqlRelationalTableModel:setFilter()

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 417 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    panosk
    wrote on last edited by
    #1

    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
       this->select();
       QSqlQuery query;
       query.prepare(QString("UPDATE %1 SET target = :text "
                           "WHERE sourceID = :id")
                   .arg(referenceTable));
    
       this->database().transaction();
    
       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(":text",sourceIndex.data().toString());
                   query.bindValue(":id",targetIndex.row()+1); // +1 = rowid in db
                   if (!query.exec()) {
                       qDebug() << "Not updated: " << query.lastError().text();
                       break;
                   }
               }
            }
       }
       this->database().commit();
       this->setFilter("sourceText != ''"); // Here I try to reapply the filter
       if (select()) {
           while(canFetchMore())
               fetchMore();
       }
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved