Solved Make TableView Editable in QT Designer form class without buttons
-
You should take a look at QSqlTableModel
-
@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.
-
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
-
@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.
-
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?
-
Hi
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
-
@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.
-
I made it workable with your help, can you tell me how to make the topic RESOLVED?
-
Hi
Good to hear :)
At the first post, there are topic tools button to the right side, and in there you can set to solved:
Also, what fixed it for you?
Just for future readers in case, they run into the same. -
i had a form where i had to click (Load Table) . i hide that form to make the tablewindow editable.