PDA

View Full Version : QStandardItemModel changes to QSqlTableModel



nagabathula
20th April 2011, 07:41
Hello i have a problem i looked a lot but did not find a exact solution. I have a
QStandardItemModel to which i am adding data which is present in the QVectors which is retrieved from the data base and i set this data Model into the tableView.

This is how i am displaying the data from a data base to QTableView

QStandardItemModel *model1 ;
model1 = new QStandardItemModel();
for(int i=0;i< temptagidnames.count(); i++)
{
QStandardItem *item = new QStandardItem(temptagidnames.at(i));
model1->setItem(i, 0, item);
}
model1->setHeaderData(0, Qt::Horizontal, tr("TAG ID"));
model1->setHeaderData(1, Qt::Horizontal, tr("Description"));
tableView->setModel(model1);
tableView->setAlternatingRowColors(true);
tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

I wrote the below code to update the changes made in the QTableView into the sqlite db.



QStandardItemModel *model1 ;
model1 = new QStandardItemModel();
QSqlTableModel *model1;
model1 = new QSqlTableModel;

model1->setTable("tempchnames");
model1->setEditStrategy(QSqlTableModel::OnFieldChange);
tableView->setColumnHidden(0, true);

for(int i=0;i< temptagidnames.count(); i++)
{
QStandardItem *item = new QStandardItem(temptagidnames.at(i));
model1->setItem(i, 0, item);
}

model1->setHeaderData(0, Qt::Horizontal, tr("TAG ID"));
model1->setHeaderData(1, Qt::Horizontal, tr("Description"));

tableView->setModel(model1);
tableView->setAlternatingRowColors(true);
tableView->setSizePolicy(QSizePolicy::Expanding,


But i get errors like, I am not able to know how i can link QStandardItemModel Model and QSqlTableModel so that the changes i make in the QTableView are reflected in the data base immediately.


error C2371: 'model1' : redefinition; different basic types
error C2039: 'setTable' : is not a member of 'QStandardItemModel'
i am stuck with this problem past 2 days,.
Thank you

mcosta
20th April 2011, 08:07
You have to delete the first two lines of code



QStandardItemModel *model1 ;
model1 = new QStandardItemModel();

nagabathula
20th April 2011, 08:43
hello thank you for the reply.. i tried it but i have one error. I checked in detailed description of QSqlTableModel there is no setItem.. i did not find any alternate for it.. what should i do i order to remove the error


error C2039: 'setItem' : is not a member of 'QSqlTableModel'

this is how i am setting the data into the model

model1->setItem(i, 0, item);

thank you

Added after 29 minutes:

hi should i use setRecord or setData to display the data i have in the model into QTableView.. ?
Pls help me out .


Thank you

mcosta
20th April 2011, 08:43
To use QSqlTableModel you need to provide the name of a database table.

After that call QSqlTableModel::select

nagabathula
20th April 2011, 09:45
hello i did not understand exactly how to do that.. You you pls show me a small example on how to do it. ..


thank you

mcosta
20th April 2011, 10:10
Here (http://doc.qt.nokia.com/4.7/qsqltablemodel.html#details) you find more information

nagabathula
20th April 2011, 12:04
Hello thank you so much for the help that solved my problem. I got what i wanted..

Regards

nagabathula
20th April 2011, 15:49
Hello i have small warning and a query error when i try to display the data in QTableView after i do the changes.. i was wondering where i was going wrong. ?


QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
i saw in the forum that i have to delete the db connection but how can i do it. should i do it in the constructor.

thank you

mcosta
21st April 2011, 10:39
To solve your problem you have to


Close database connection
Delete Model object(s)
Remove Database


For Example



QString connName;
{
QSqlDatabase db = model->database();
connName = db.connectionName();

db.close (); // 1

delete model; // 2.
model = 0;
}
QSqlDatabase::removeDatabase(connName);