【问题标题】:Inserting row into QSqlTableModel将行插入 QSqlTableModel
【发布时间】:2018-04-29 08:07:53
【问题描述】:

当要编辑一行时,会将索引传递到编辑对话框中。
编辑工作正常。
当我想添加一行时,我不会将索引传递给对话构造函数,因此它知道它应该添加一个新行。
这是对对话按钮做出反应的函数代码

void DialogAddCl::on_buttonBox_clicked(QAbstractButton *button)
{
    // prepare
    m->database().transaction();
    QString debugstr;
    auto chi4 = ui->buttonBox->buttonRole(button);
    int rowCount = m->rowCount();
    switch (chi4) {
    case QDialogButtonBox::AcceptRole:
        if (!ind->isValid())
            // insert
            if (!m->insertRow(rowCount, *ind))
            {
                QMessageBox::critical (this, "Error inserting into model", m->lastError().text(), QMessageBox::Cancel);
                break;
            }
            else
            {
                m->setData(m->index(rowCount, 0), rowCount+1);
            }
        else
        {
            // update
            rowCount = ind->row();
        }
        m->setData(m->index(rowCount, 1), ui->name->text());
        m->setData(m->index(rowCount, 2), ui->addr->text());
        // 12 other setData() calls

        if (m->submitAll())
        {
            m->database().commit();
        }
        else
        {
            // rollback if error
            m->database().rollback();
            debugstr = QString(m->database().lastError().text());
            QMessageBox::critical (this, "Database returned an error",
                                   m->database().lastError().text(), QMessageBox::Cancel);
        }
    case QDialogButtonBox::RejectRole:
        this->close();
        break;
    case QDialogButtonBox::ResetRole:
        fillFields();
        break;
    default:
        break;
    }

}

这是一段dialogaddcl.h:

private:
    Ui::DialogAddCl *ui;
    QSqlTableModel* m;
    QModelIndex* ind;

所以,m->submitAll() 在我编辑现有记录时工作正常,如果我尝试提交新插入的行则失败。我已经检查了调试,setData() 调用即使在添加时也能正常工作,所以它不是 DB 期待 NOT NULL 字段并给出错误。
顺便说一句,也许有人可以指出一种捕获实际错误文本的方法?我试过debugstr,但它总是只包含空字符串。我打印的错误消息也是如此

【问题讨论】:

    标签: c++ qt qt5 qtsql qsqltablemodel


    【解决方案1】:

    若要在QSqlTableModel 中插入一行,则不应直接使用setData(),如果不能使用insertRecord(),则在此处指明行和QSqlRecordQSqlRecord可以通过模型的record()方法获取。

    InsertDialog dial;
    if(dial.exec()== InsertDialog::Accepted){
        db.transaction();
        QString f = dial.firstname();
        QString l = dial.lastname();
    
        QSqlRecord record = model.record();
        /* since the id field has the autoincrement attribute,
         * it is not necessary to indicate its value,
         * that is because this field of the request is removed.
        */
        record.remove(record.indexOf("id"))
        record.setValue("firstname", f);
        record.setValue("lastname", l);
        /*-1 is set to indicate that it will be added to the last row*/
        if(model.insertRecord(-1, record)){
            qDebug()<<"successful insertion";
            model->submitAll();
        }
        else{
            db.rollback();
        }
    }
    

    在下面的link你会找到一个例子。

    【讨论】:

    • 等等,但是我没有使用 setData 来插入,我用它来设置新插入的行中的值。我在 setData 之前调用 insertRow
    • 如果无法使用 insertRow(s),我完全接受。只需提供任何链接来证明这一点,我将切换到使用 insertRecord
    • 是不是不用setData不用insertRow,只要用insertRecord,这个方法内部调用前面的。
    • 我现在明白了。但是,我不需要删除 ID,因为它不是自动递增的。我会改变那个细节。感谢您澄清什么叫什么
    • @mekkanizer 好的,就我而言,如果我这样做了,那只是一个加分项。 :P
    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2017-01-16
    • 2020-12-04
    • 2023-01-07
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多