QStandardItemModel changes to QSqlTableModel
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
Code:
for(int i=0;i< temptagidnames.count(); 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);
I wrote the below code to update the changes made in the QTableView into the sqlite db.
Code:
model1->setTable("tempchnames");
tableView->setColumnHidden(0, true);
for(int i=0;i< temptagidnames.count(); 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);
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.
Quote:
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
Re: QStandardItemModel changes to QSqlTableModel
You have to delete the first two lines of code
Re: QStandardItemModel changes to QSqlTableModel
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
Quote:
error C2039: 'setItem' : is not a member of 'QSqlTableModel'
this is how i am setting the data into the model
Quote:
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
Re: QStandardItemModel changes to QSqlTableModel
To use QSqlTableModel you need to provide the name of a database table.
After that call QSqlTableModel::select
Re: QStandardItemModel changes to QSqlTableModel
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
Re: QStandardItemModel changes to QSqlTableModel
Here you find more information
Re: QStandardItemModel changes to QSqlTableModel
Hello thank you so much for the help that solved my problem. I got what i wanted..
Regards
Re: QStandardItemModel changes to QSqlTableModel
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. ?
Quote:
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
Re: QStandardItemModel changes to QSqlTableModel
To solve your problem you have to
- Close database connection
- Delete Model object(s)
- Remove Database
For Example
Code:
{
connName = db.connectionName();
db.close (); // 1
delete model; // 2.
model = 0;
}