开发者

QDataWidgetMapper, QSqlRelationalTableModel, mapper won't submit

I am facing a problem with a C++ Qt4 Widget. This widget is to be used for adding rows into a SQLite database. Here are the database tables which are used in the problem I'm facing:

CREATE TABLE 'statuses' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , 'name' VARCHAR(50) NOT NULL UNIQUE , 'bgcolor' VARCHAR(6) NOT NULL DEFAULT ffffff, 'fgcolor' VARCHAR(6) NOT NULL DEFAULT 000000)
CREATE TABLE 'addresses' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , 'name' VARCHAR(50) NOT NULL UNIQUE , 'country' VARCHAR(25), 'town' VARCHAR(50), 'postal_code' VARCHAR(10), 'street' TEXT check(typeof('street') = 'text') , 'ref' VARCHAR(5) NOT NULL , 'status_id' INTEGER NOT NULL, FOREIGN KEY(status_id) REFERENCES statuses(id))

The code I wrote, which follows exactly the Qt tutorial SQL WidgetMapper, follows:

CreateAddressDialog::CreateAddressDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CreateAddressDialog)
{
    ui->setupUi(this);
    /* Set up "buddies" */
    ui->nameLabel->setBuddy(ui->nameLineEdit);
    ui->refLabel->setBuddy(ui->refLineEdit);
    ui->townLabel->setBuddy(ui->townLineEdit);
    ui->streetLabel->setBuddy(ui->streetLineEdit);
    ui->countryLabel->setBuddy(ui->countryLineEdit);
    ui->postalCodeLabel->setBuddy(ui->postalCodeLineEdit);
    ui->statusLabel->setBuddy(ui->statusCB);

    /* Set up status model */
    ui->statusCB->clear();
    statusModel = new QSqlRelationalTableModel(this, backbone::instance()->db);
    statusModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    statusModel->setTable("addresses");
    int statusFieldId = statusModel->fieldIndex("status_id");
    /***** Warning: after next instruction fieldIndex('status_id') will return -1. */
    statusModel->setRelation(statusFieldId,QSqlRelation("statuses", "id", "name"));
    statusModel->select();
    relModel = statusModel->relationModel(statusFieldId);
    ui->statusCB->setModel(relModel);
    ui->statusCB->setModelColumn(relModel->开发者_JS百科;fieldIndex("name"));
    mapper = new QDataWidgetMapper(this);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    mapper->setModel(statusModel);
    mapper->addMapping(ui->refLineEdit, statusModel->fieldIndex("ref"));
    mapper->addMapping(ui->nameLineEdit, statusModel->fieldIndex("name"));
    mapper->addMapping(ui->statusCB, statusFieldId);
}

The submission process is as simple as the following:

void CreateAddressDialog::accept()
{
    if(ui->nameLineEdit->text().length() < 5){
        QMessageBox::critical(0, tr("Error"),tr("The name must be at least 5 characters long."), QMessageBox::Cancel);
        return;
    }else if(ui->refLineEdit->text().length() > 10){
        QMessageBox::critical(0, tr("Error"),tr("The reference may not be more than 10 characters long."), QMessageBox::Cancel);
        return;
    }else if(ui->statusCB->currentIndex() < 0){
        QMessageBox::critical(0, tr("Error"),tr("The item must have a status."), QMessageBox::Cancel);
        return;
    }
    qDebug() << mapper->submit();
    this->close();
}

The problem, is that mapper->submit() failed ("false" is displayed). If I call the model's lastError, nothing shows up. The fields which are mapped to this mapper are the mandatory fields of the table (id excepted because it is marked as autoincrement).

Any help is greatly appreciated as I really don't understand (and can't seem to find out) why this widget doesn't add anything to the database. Let me mention that I checked with a standard SQLite database viewer to be sure of the contents of the SQLite database were like.

P.S.: You can browse the full code for reference at https://code.google.com/p/invensile/source/browse/#svn%2Ftrunk

Thanks in advance!


Although the question is very old, but I have experienced the same thing few days ago,

statusModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

the argument OnManualSubmit of the line is forcing you to manually submit contents to database.So, to make changes in the database I had to call statusModel->submitAll() after calling mapper->submit().


Try using submitAll instead of submit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜