PDA

View Full Version : The primeInsert() gets called only once



LaTj
5th September 2013, 05:48
I am making a database that adds a person's name and info. I am a newbie, so I will try to explain my problem.

I used the insertRows() function so the primeInser signal can be emmited. The problem is that it only executes only once. I am using an QSqlTableModel and QTableView to make the database.

//Here is the code on the constructor

nameModel = new QSqlTableModel(this);
nameModel->setTable("member");
nameModel->setEditStrategy(QSqlTableModel::OnFieldChange);
nameModel->setSort(Member_Name, Qt::AscendingOrder);
nameModel->setHeaderData(Member_Name, Qt::Horizontal, tr("Name"));
nameModel->setHeaderData(Member_Gender, Qt::Horizontal, tr("Gender"));
nameModel->setHeaderData(Member_DOB, Qt::Horizontal, tr("DOB"));
nameModel->setHeaderData(Member_PersonalInfo, Qt::Horizontal, tr("Personal Info"));
nameModel->select();

nameTableView = new QTableView;
nameTableView->setModel(nameModel);
nameTableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
nameTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
nameTableView->resizeColumnsToContents();

//After calling insertRows

int row = nameModel->rowCount();

nameModel->insertRows(row, 1);



//then my insertName signal is called

void MainForm::insertName(int row, QSqlRecord &record)
{
record = nameModel->record(row);
record.setValue("id", generateId("member"));
record.setValue("name", nameform->getNameText());
record.setValue("gender", nameform->getGenderText());
record.setValue("dob", nameform->getDOBDate());
record.setValue("persinfo", nameform->getPrivacyText());

}

//my problem is that it only creates and displays the first row and on the second insertRows(row, 1) it does not emit the primeInsert() and it only displays the
//first row.

//If anyone could help me, it would be greatly appreciated.

wysota
6th September 2013, 11:27
Why are you overwriting the record given by the signal with your own? You are supposed to modify the record you were given.

LaTj
6th September 2013, 20:05
Thanks for your advice. I know I am doing a lot of rookie mistakes so your questions and answers are well accepted.

Added after 30 minutes:

okay, Now the signal forces me to use a slot that uses the row parameter. So, I added

nameModel->setRecord(row, record);

at the end of the insertName(int row, QSqlRecord &record).

I still have the same problem where the insertRows does not call my insertName() function. Am I missing some code like select or selectRows, submit, submitAll?

wysota
6th September 2013, 20:55
So, I added

nameModel->setRecord(row, record);

at the end of the insertName(int row, QSqlRecord &record).
What for? You are told to modify the record you get as a reference and that's that. No need to set that record, QSqlTableModel will do that when it feels like it.

If you ask me then your whole approach doesn't make sense. You should call insertRow(), get the index of that row and use setRecord() to modify the row. There is no need to tap into primeInsert(), I think you're missing the point what it is for. It is intended to modify an empty record before it is inserted into the database so that it is compliant with the database schema (e.g. all not null columns get values).

LaTj
6th September 2013, 23:01
It works now, thanks . You were right my problem was not understanding what primeInsert is intended for.