Change cell data in tableview QT
-
I am trying to pull data's in database then print on tableview then change them one by one. I can reach each of the cells (in later times I'll iterate it via for whatever..) but I can't change them. This is the code when I press the button, it shows database data's without a problem but I can't change them. How can I achieve this?
void manager::on_pushButton_show_clicked() { connOpen(); QSqlQueryModel * modal = new QSqlQueryModel(); QSqlQuery* qry = new QSqlQuery(mydb); qry->prepare("select * from data"); qry->exec(); modal->setQuery(*qry); ui->tableView->setModel(modal); //up to now, there is no problem, table appears as expected.. QString cellstring = ui->tableView->model()->data(ui->tableView->model()->index(0,2)).toString(); qDebug() << cellstring; //I can read the cell's data, *cellstring* holding it QString newstring = "hello"; *1 ui->tableView->model()->setData(ui->tableView->model()->index(0,2), newstring); *2 QString another = ui->tableView->model()->data(ui->tableView->model()->index(0,2)).toString(); qDebug() << another;in number1 line I try to change the data inside the cell.
in number2 line I read the cell's data then print it, but there is no change at all.
I still see the old variable;It prints: abcdefgh
It should print: helloIs the setData method does not work properly? Any suggestions? Thanks.
I am newbie here, I wish I clearly express myself.. -
From the document of
QSqlQueryModel:The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
-
From the document of
QSqlQueryModel:The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
@Bonnie Is there an example for this? I did not implement subclass. Thx.
Edit: I want to fetch database then do some calculations on it then print it via table. If I use QSqlTableModel original database variables will change? It should not be change.
Edit2: Yes it was. I changed the table in the app, and there is no changing in database. Thx Bonnie..
-
From the document of
QSqlQueryModel:The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
-
@Bonnie Is there an example for this? I did not implement subclass. Thx.
Edit: I want to fetch database then do some calculations on it then print it via table. If I use QSqlTableModel original database variables will change? It should not be change.
Edit2: Yes it was. I changed the table in the app, and there is no changing in database. Thx Bonnie..
@hubeytqew Well, actually it can change the database, and the behaviour is depending on the
QSqlTableModel::editStrategy().
If you don't want original database changed, make sure to set the edit strategy toQSqlTableModel::OnManualSubmit.
Then it will only try to modify the database when you callQSqlTableModel::submitAll().
You really need to look through the documents before asking questions :)