我遇到了C ++ Qt4 Widget的问题。此小部件用于将行添加到SQLite数据库中。以下是我遇到的问题中使用的数据库表:
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))
我编写的代码完全遵循Qt教程SQL WidgetMapper,如下所示:
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->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);
}
提交过程非常简单:
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();
}
问题在于mapper-&gt; submit()失败(显示“false”)。如果我调用模型的lastError,则不会显示任何内容。映射到此映射器的字段是表的必填字段(id除外,因为它标记为自动增量)。
我非常感谢任何帮助,因为我真的不明白(并且似乎无法找到)为什么这个小部件不会向数据库添加任何内容。让我提一下,我检查了一个标准的SQLite数据库查看器,以确保SQLite数据库的内容就像。
P.S。:您可以在https://code.google.com/p/invensile/source/browse/#svn%2Ftrunk
浏览完整代码以供参考提前致谢!
答案 0 :(得分:0)
虽然这个问题很老,但我前几天经历过同样的事情,
statusModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
该行的参数OnManualSubmit
强制您手动将内容提交到数据库。因此,要在数据库中进行更改,我必须在调用statusModel->submitAll()
后调用mapper->submit()
。
答案 1 :(得分:-1)
尝试使用submitAll而不是提交。