PDA

View Full Version : Inserting Record



Nefastious
19th October 2009, 18:34
Hi there guys!

I'm having a small problem here, whenever i try to insert a QSqlRecord into a QSqlTableModel, I know that a new row is inserted (QTableView shows that) but data simply isn't displayed in the view (the fields remain blank). Here is the code:

in mainw.cpp

//this makes the adddialog SLOT to instantiate an "Add" dialog
void mainwindow::adddialogslot() {
adddialog *add = new adddialog;
add->show();
if (add->Accepted) {
model->insertRecord(-1,add->record);
}
}

in adddialog.cpp

void adddialog::allowinput() {

if (lineEdit_name->text().isEmpty() || lineEdit_surname->text().isEmpty()
|| lineEdit_phone->text().isEmpty() || lineEdit_phone1->text().isEmpty()
|| lineEdit_phone2->text().isEmpty() || lineEdit_phone3->text().isEmpty()
|| lineEdit_address->text().isEmpty() || lineEdit_year->text().isEmpty()
|| lineEdit_insurance->text().isEmpty() || lineEdit_HS->text().isEmpty()
|| lineEdit_diseases->text().isEmpty() || lineEdit_treat->text().isEmpty()
|| lineEdit_comp->text().isEmpty() || comboBox_day->currentText().isEmpty()
|| comboBox_month->currentText().isEmpty() || !(radioButton_M->isChecked() || radioButton_F->isChecked()) ) {
QMessageBox::information(this, tr("Add Patient"), tr("Please fill in all of the required fields"));
}
else {
record.setValue("name", lineEdit_name->text());
record.setValue("surname", lineEdit_surname->text());
if (radioButton_M->isChecked()) {
record.setValue("gender", "M");
}
else {
record.setValue("gender", "F");
}
record.setValue("birth date", comboBox_day->currentText() + "/" + comboBox_month->currentText() +"/"+ lineEdit_year->text());
record.setValue("address", lineEdit_address->text());
record.setValue("phone number", lineEdit_phone->text() + lineEdit_phone1->text() + lineEdit_phone2->text() + lineEdit_phone3->text());
record.setValue("insurance number", lineEdit_insurance->text());
record.setValue("health system", lineEdit_HS->text());
record.setValue("diseases", lineEdit_diseases->text());
record.setValue("treatments", lineEdit_treat->text());
record.setValue("complications", lineEdit_comp->text());
}
}

What am I doing wrong? Any code samples would be greatly appreciated.

Thanks in advance,

Nefastious :confused:

Lykurg
19th October 2009, 18:55
Are you sure, your record is valid? A field name with a white space like "insurance number" seems odd to me and maybe you need to set the integer index and not only the name? "Debug" a record which was created by the model and check it with the one you create.

Nefastious
19th October 2009, 19:30
Well using the following:


if (record.isEmpty()) {
qDebug() << "It is empty";
}

I am informed that the record is indeed empty.
What exactly am I doing wrong? Instead of name should I have specified the index instead?

Thanks in advance

ChrisW67
20th October 2009, 04:28
//this makes the adddialog SLOT to instantiate an "Add" dialog
void mainwindow::adddialogslot() {
adddialog *add = new adddialog;
add->show();
if (add->Accepted) {
model->insertRecord(-1,add->record);
}
}
Line 4 opens a modeless dialog and immediately returns control. The user doesn't a have chance to do anything in the dialog before you grab the (empty) record and insert it. You probably wanted:

add->exec(); to open a modal dialog to wait for user data entry and confirmation.