PDA

View Full Version : QSqlTableModel Insertion Problem



Pluvius
21st December 2011, 13:44
Hello everyone,

I'm working on a project and in the "add" feature I want to show a single row from a QSQlTableModel where the user can input data, on a "simple" database table, it worked like a charm, but when I wanted to replicate the same pattern on my project It didn't behave as expected, here's the scenario:

1- the program is launched, displaying a newly added row and hiding the previous ones

2- The user adds data and submits it through a button

3- A slot is called through the button to submit the newly added data, hide that row and display a new one

Here's the code of the slot:



{
model->submit();

int row = model->rowCount();
for (int i=0;i<row;i++)
ui->tabsave->hideRow(row); //hide the previous rows

model->setData(model->index(row,5),QTime::currentTime());


}

Here's the code regarding the formatting and display of the Model




ui->tabsave->setModel(model);
//model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();

for (int i=16;i<20;i++)
{
ui->tabsave->hideColumn(i); //hiding some columns from the user
}


int row = model->rowCount();
for (int i=0;i<row;i++)
{
ui->tabsave->hideRow(i);

}
model->insertRow(row);
model->setData(model->index(row,5),QTime::currentTime());
ui->tabsave->setRowHeight(row,80);

////////////////SLOTS//////////////

QObject::connect(ui->next_one,SIGNAL(clicked()),this,SLOT(added_next()) );


Voila, Hope I'll get some help around here. Thank you very much

ChrisW67
21st December 2011, 22:50
It didn't behave as expected how? Crashed, failed to write to the actual database, failed to hide rows, wrote rows with rubbish data, wept uncontrollably for Kim Jong-Il ;)

QSqlTableModel::submit() does not necessarily write things to the database, depending on your QSqlTableModel::editStrategy()

Pluvius
22nd December 2011, 16:17
haha! Like the Kim hint xD!

Well I've managed to solve the problem (partially!)

Now everytime I invoke the "next" slot, I get one nice row, where the user can input data, the unsolved part,however, is that I cannot write that row to the database.

In another example, I didn't set any edit strategy, and it worked fine, but with this particular model, I need some sort of control, I tried


model->setEditStrategy(QSqlTableModel::OnManualSubmit);

and later on in the slot I would issue


model->submitAll();

Yet no data is written, I'm confused with this one!

ChrisW67
23rd December 2011, 01:33
It could be failing because the data you are trying to write violates table constraints. For example, if a certain column must be not null, or a primary key field is not populated or a duplicate, a foreign key constraint is violated, or a value fails a check constraint.

Pluvius
23rd December 2011, 06:06
Thanks a lot Chris! You rule!

I hid some columns, and wasn't paying attention that I was submitting QStrings into TinyInt fields, Thanks again man :)

EDIT: How can I label the thread asa [SOLVED] ?