PDA

View Full Version : QTableView, add new record?



grellsworth
3rd July 2007, 15:04
In most database applications that I've seen, the widget that displays the table data has an extra (blank) row at the bottom that the user can use to insert/append a new record into the database.

How can I do that with a QTableView?

wysota
4th July 2007, 16:48
What did you already try?

grellsworth
4th July 2007, 18:12
I haven't tried anything... I don't know what to try, that's why I'm asking.

- Gordon E.

jpn
4th July 2007, 18:34
One option could be to subclass one of QSqlXxxModels, fake an extra row by reimplementing rowCount() and setting index widgets or opening persistent editors at the bottom row. Depending on types of editors, you could for example connect to QLineEdit::returnPressed() and insert the row on demand.

wysota
4th July 2007, 23:11
In my opinion you don't really need to do anything - all that is needed is to call insertRow() on the model and edit the row (either automatically or letting the user start the editor). That's why I asked the question in the first place.

grellsworth
5th July 2007, 12:04
In my opinion you don't really need to do anything - all that is needed is to call insertRow() on the model and edit the row (either automatically or letting the user start the editor). That's why I asked the question in the first place.

OK, I've tried adding a new row into my model, this looks like it might work. I'll let you know. I'm just concerned that after a new row is inserted, that a new blank line will appear.

Thank you for your help. I'll keep you posted on how it's going.

Sincerely,

Gordon E.

===== UPDATE =====

Here's what I'm doing right now:


void SQLiteDB::addRow(QTableView *tableView)
{
QAbstractItemModel *model = tableView->model();

model->insertRow(model->rowCount());
}

This works fine for adding the initial empty row at the bottom of the table, but now I have to find a way to make sure that when the user successfully enters a new record, a new empty row will be added at the end.

Suggestions?

Sincerely,

Gordon E.

wysota
5th July 2007, 15:08
Use signals and slots to connect to some signals where you will check whether the row has been successfully edited and create a new row.