Make TableView Editable in QT Designer form class without buttons
-
wrote on 9 Jan 2021, 09:55 last edited by Ahsan Niaz 1 Sept 2021, 10:21
Hi, I am a newbie in QT environment.
I have created an application using QT Designer Form Class. I have multiple forms, and these forms are connected to each other end to end. The last form I have is connected to database successfully. In the UI of this form, I have two major things.- Push button to load data from sqlite table
- TableView to show the loaded data
There are two major things i want to do,
- I want to make tableview editable, i.e if i double click a cell in tableview, it should allow me to edit the value of that cell.
- I have tried setedittriggers etc, but nothing worked. I also tried to change the triggers from the UI, but it never worked.
- Secondly, if we succeed in making the tableview editable, i want to upload the edited table back to my database.
This is what i have done by now, which working fine but not editing the tableview cells
void Case_Adjustment::on_pushButton_clicked() { Case_Adjustment co; QSqlQueryModel * modal = new QSqlQueryModel(); QSqlQuery * qry = new QSqlQuery(co.mydb); qry->prepare("select * from comparison"); qry->exec(); modal->setQuery(*qry); //table.setModel((modal)); ui->tableView->setModel(modal); }
Is there anyone who can help in this regard? I will be really thankful
-
You should take a look at QSqlTableModel
-
Hi, I am a newbie in QT environment.
I have created an application using QT Designer Form Class. I have multiple forms, and these forms are connected to each other end to end. The last form I have is connected to database successfully. In the UI of this form, I have two major things.- Push button to load data from sqlite table
- TableView to show the loaded data
There are two major things i want to do,
- I want to make tableview editable, i.e if i double click a cell in tableview, it should allow me to edit the value of that cell.
- I have tried setedittriggers etc, but nothing worked. I also tried to change the triggers from the UI, but it never worked.
- Secondly, if we succeed in making the tableview editable, i want to upload the edited table back to my database.
This is what i have done by now, which working fine but not editing the tableview cells
void Case_Adjustment::on_pushButton_clicked() { Case_Adjustment co; QSqlQueryModel * modal = new QSqlQueryModel(); QSqlQuery * qry = new QSqlQuery(co.mydb); qry->prepare("select * from comparison"); qry->exec(); modal->setQuery(*qry); //table.setModel((modal)); ui->tableView->setModel(modal); }
Is there anyone who can help in this regard? I will be really thankful
wrote on 9 Jan 2021, 10:13 last edited by JonB 1 Sept 2021, 10:16@Ahsan-Niaz said in Make TableView Editable in QT Designer form class without buttons:
I have tried setedittriggers etc, but nothing worked. I also tried to change the triggers from the UI, but it never worked.
Well, it should do, so you had better show your attempts. (I think the defaults are editable on double-click, without you needing to make any changes on edit triggers.) You should be hooking the
QTableView
to aQSqlTableModel
which connects to the SQLite database. Once this works all of your requirements should be satisfied.Make sure you have read the conceptual https://doc.qt.io/qt-5/model-view-programming.html. A whole, simple example for just your situation is https://doc.qt.io/qt-5/qtsql-tablemodel-example.html.
-
wrote on 9 Jan 2021, 10:16 last edited by
I have set
- editTriggers to QAbstractItemView::NoEditTriggers
- selectionBehavior to QAbstractItemView::SelectRows
But i couldn't edit the cells, I am using Qt Creator to build my application
-
I have set
- editTriggers to QAbstractItemView::NoEditTriggers
- selectionBehavior to QAbstractItemView::SelectRows
But i couldn't edit the cells, I am using Qt Creator to build my application
wrote on 9 Jan 2021, 10:21 last edited by JonB 1 Sept 2021, 10:21@Ahsan-Niaz said in Make TableView Editable in QT Designer form class without buttons:
editTriggers to QAbstractItemView::NoEditTriggers
So that will obviously prevent you from clicking-to-edit, which you stated you want:
I want to make tableview editable, i.e if i double click a cell in tableview, it should allow me to edit the value of that cell.
So why set no edit triggers, and then wonder why it's not editable??
Anyway, I linked you to a 30-line example code which works.
-
wrote on 9 Jan 2021, 10:34 last edited by
Thanks for your Guide MR. JonB
I have used thisQSqlTableModel *model = new QSqlTableModel; model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QTableView *view = new QTableView; view->setModel(model); view->hideColumn(0); // don't show the ID view->show();
which has successfully loaded the data to a separate window where cells are editable. What do i need to do to upload the edited table back to my database? Any hints?
-
Thanks for your Guide MR. JonB
I have used thisQSqlTableModel *model = new QSqlTableModel; model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QTableView *view = new QTableView; view->setModel(model); view->hideColumn(0); // don't show the ID view->show();
which has successfully loaded the data to a separate window where cells are editable. What do i need to do to upload the edited table back to my database? Any hints?
Lifetime Qt Championwrote on 9 Jan 2021, 10:39 last edited by mrjj 1 Sept 2021, 10:40Hi
Since you have set
QSqlTableModel::OnManualSubmit
then you need to call summitAll to make it write it back.
https://doc.qt.io/qt-5/qsqltablemodel.html#submitAllOr simply dont have it set to manual.
https://doc.qt.io/qt-5/qsqltablemodel.html#EditStrategy-enum
-
wrote on 9 Jan 2021, 10:49 last edited by
@mrjj I am really sorry but i couldn't get this, I ran the following code
QSqlTableModel *model = new QSqlTableModel; model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QTableView *view = new QTableView; view->setModel(model); view->show();
it ran well, without errors, created a separate window, loaded the table perfectly, when i double clicked a cell, it seemed to be editable, but it doesn't get any keyboard input,
Saying again, it looks like it is editable when i double click, but it is not taking any input from the keyboard.
I have also changed OnManualSubmit to OnRowChange -
@mrjj I am really sorry but i couldn't get this, I ran the following code
QSqlTableModel *model = new QSqlTableModel; model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QTableView *view = new QTableView; view->setModel(model); view->show();
it ran well, without errors, created a separate window, loaded the table perfectly, when i double clicked a cell, it seemed to be editable, but it doesn't get any keyboard input,
Saying again, it looks like it is editable when i double click, but it is not taking any input from the keyboard.
I have also changed OnManualSubmit to OnRowChange@Ahsan-Niaz
Hi
Sorry, I dont know what that could be as if it goes into edit mode then you should also be able to press keys in it.I did fast test and it edits fine.
-
wrote on 9 Jan 2021, 11:21 last edited by
I made it workable with your help, can you tell me how to make the topic RESOLVED?
-
I made it workable with your help, can you tell me how to make the topic RESOLVED?
Lifetime Qt Championwrote on 9 Jan 2021, 11:28 last edited by mrjj 1 Sept 2021, 11:30 -
wrote on 9 Jan 2021, 11:31 last edited by
i had a form where i had to click (Load Table) . i hide that form to make the tablewindow editable.
1/12