PDA

View Full Version : QTableView and QSQLTableModel



raphaelf
4th March 2006, 13:33
Hello everybody

QT:4.1.1

I am able with this code to insert a Row in my table:


QString table = ui.tabelle_le->text();
QSqlTableModel *model = new QSqlTableModel;
model->setTable(table);
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();

QSqlRecord rec = model->record();
ui.tableView->setModel(model);
ui.tableView->show();


rec.setValue("status", "test");
model->insertRecord(-1, rec);


My problem is, that i have some tables, so i am searching a solution how to implement my function so that i have not to write every columns like:
table1


rec.setValue("status", "test");
rec.setValue("date", "test");
rec.setValue("price", "test");
.
.
.

table2


rec.setValue("address", "test");
rec.setValue("tel", "test");
rec.setValue("mobil", "test");
.
.
.


I would like to insert a empty row: modal->insertRecord(-1, rec); fill cells and save it
Can somebody see the problem? Have somebody a idea how to implement this dynamic?

wysota
4th March 2006, 14:10
Maybe something like this:


QList< QPair< QString, QVariant > >list;
list << QPair<QString, QVariant>("fld1", 10);
list << QPair<QString, QVariant>("fld2", "xxx");
list << QPair<QString, QVariant>("fld3", "2005-01-01");
//...

QSqlRecord rec = model->record();
foreach(QPair<QString, QVariant> item, list){
rec.setValue(item.first, item.second);
}
// ...


Of course you can construct the list in any way you want. You could use a QMap instead and have something like this:


QMap<QString, QVariant> map;
map["fld1"] = 10;
map["fld2"] = "xxx";
map["fld3"] = "2005-01-01";
//...
foreach(QString key, map.keys()){
record.setValue(key, map[key]);
}
//...

raphaelf
4th March 2006, 14:49
Hi Wysota,
I think i have not explain exactly again my problem :rolleyes:
or i cant see what you mean :rolleyes:

ok,

I have a combobox with all my tables names from database x.
I have a tableview where i can show my table by clicking a button.

I want to click a button and insert a empty row in my tableView.
The User can fill the cells (Maybe 2, or 18 colums) and save Database
I am searching a solution how to insert this row in the table without to implement fix in my code :


//status is a column name, i dont want to implement all columns from my database
rec.setValue("status", "test");


Its not possible to implement something dynamic like this:
1. Show table that i want to insert a new row
2. insert a empty row in my QTableView
3. User can fill the fileds
4. getColumnnames from current Table ind insert Values(cell1, cell2)

If you mean something like this, i have to understand your code :rolleyes:
Can you see my problem?If not i have to explain again :(

Maybe this function is allready implemented by qt but i dont know which one

wysota
4th March 2006, 16:16
You have to explain again :) I don't have the slightest idea what your problem is :)

Can't you just use an editable model and let Qt handle everything?

What is exactly your problem? You don't know the field names or what?

raphaelf
4th March 2006, 16:38
Its not easy to bring this on text :p

I try again:

I am testing QSqlTableView class for update, delete and insert a new dataset (record) in a Table.

My situation now:
i am able to update a Table like this (really easy, i had just to change the EditStrategy ;) ):


QString table = ui.tabelle_le->text();
QSqlTableModel *model = new QSqlTableModel;
model->setTable(table);
//So kann man jeder Feld update: OnFiledChange
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
ui.tableView->setModel(model);
ui.tableView->show();

i am able too, to delete a dataset like this(very simple):


.
.
model->removeRows(x,1);

What i need now, is to find a simple way, how to insert a new dataset over my tableView
I now that i can use this:


rec.setValue("status", "test");
model->insertRecord(-1, rec);

So my problem is:
1. I have 18 tables
2. every table has between 2 and 23 columns.
3. How to insert a new dataset over my TableView in a easy Way independent of which table is set as Modal? ;)

I hope i could explain better, if not i shoul lern more english:rolleyes:

wysota
4th March 2006, 17:16
Just insert an empty row into your model using insertRow() or insertRecord() with an empty record and order the view to go into edit mode on the newly inserted row.

raphaelf
4th March 2006, 18:09
Hi Wysota!
finally i am able now to input a new rec :p
I hope you mean like that:


void MainWindow::insertNewRow()
{
QString table = ui.tabellen_cb->currentText();
QSqlTableModel *model = new QSqlTableModel;
model->setTable(table);
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();

ui.tableView->setModel(model);
ui.tableView->show();

//Just insert an empty row into your model using insertRow() or insertRecord()
//with an empty record
QSqlRecord rec = model->record();
model->insertRecord(-1, rec);

//and order the view to go into edit mode on the newly inserted row.
model->setEditStrategy(QSqlTableModel::OnRowChange);
}