QSqlTableModel problem with set data



  • db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test.db");
    db.open();
    QSqlQuery q(db);
    q.prepare("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY AUTOINCREMENT, Date_O VARCHAR(255));");
    q.exec();
    model = new QSqlTableModel(this, db);
    model->setTable("test");
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->select();
    ui.tableView->setModel(model);
    connect(ui.add_b, &QPushButton::clicked, [&](){model->insertRow(0); });
    

    After few clicks on button inserted only one row
    ++ after edit fields data is not stored in table.
    What could be the problem?

    Thanks in advance!


  • Lifetime Qt Champion

    Hi,

    For newly added row, OnFieldChange behaves like OnRowChange. See more information here.



  • @SGaist thanks for reply,
    but, how i can insertRow row with AUTOINCREMENT ?
    example, insertRow() -> insert empty row with AUTO INCREMENT ID.
    is it possible without reimplementation?


  • Lifetime Qt Champion

    Either trigger the submit yourself or fill all fields in the row.


Log in to reply