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. QSqlRelationalTableModel::setRecord returns TRUE but isn't working
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalTableModel::setRecord returns TRUE but isn't working

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 581 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.
  • dijunaD Offline
    dijunaD Offline
    dijuna
    wrote on last edited by
    #1
    bool NullableModel::setNullData(const QModelIndex& idx)
    {
        qDebug() << "from:" << this->record(idx.row()).field(idx.column()).value();
        QSqlRecord record = this->record(idx.row());
        QSqlField field = record.field(idx.column());
        field.clear();
        record.replace(idx.column(), field);
        qDebug() << "through:" << record.field(idx.column()).value();
        bool b = this->setRecord(idx.row(), record);
        qDebug() << "to:" << this->record(idx.row()).field(idx.column()).value();
        qDebug() << "return:" << b;
        return b;
    }
    

    When I run it and the value is already NULL, it seems fine:

    • from: QVariant(QString, "")
    • through: QVariant(QString, "")
    • to: QVariant(QString, "")
    • return: true

    If I want to really clear something, QSqlRelationalTableModel::setRecord also returns TRUE but the value isn't changing:

    • from: QVariant(int, 1)
    • through: QVariant(QString, "")
    • to: QVariant(int, 1)
    • return: true

    The real problem it that when I'm trying to set NULL value to the model, and the related field is foreign key of left-joined table, QSqlRelationalTableModel::setData returns FALSE:

    bool NullableModel::setNullData(const QModelIndex& idx)
    {
        //const QVariant value = QVariant(QString(""));
        //const QVariant value = QVariant(record().field(col).type());
        const QVariant value = QVariant(0);
        return QSqlRelationalTableModel::setData(idx, value, Qt::EditRole);
    }
    

    It returns FALSE even if I'm sure is already NULL and I'm trying to set it to the same value:

    bool NullableModel::setNullData(const QModelIndex& idx)
    {
        return QSqlRelationalTableModel::setData(idx, idx.data(Qt::EditRole), Qt::EditRole);
    }
    

    I tried everything and didn't find any solution in the Internet so it's probably some limitation of this function... so I figured I'll try to go around and set the record manually... but it's still not working and that's my question: why QSqlRelationalTableModel::setRecord returns TRUE while the record is then unchanged and how to set the data to NULL?

    This is how I create the model:

    model = new NullableModel(ui->tableView, QSqlDatabase::database());
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setTable("People");
    model->setFilter(QString("People.venue_id=%1").arg(QString::number(venue_ptr->get_id())));
    role_idx = model->fieldIndex("role_id");
    
    model->setRelation(role_idx, QSqlRelation("Roles", "id", "role_name"));
    model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    if (!model->select()) {
        showError(model->lastError());
        return;
    }
    
    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