Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Make TableView Editable in QT Designer form class without buttons



  • 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.

    1. Push button to load data from sqlite table
    2. 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


  • Lifetime Qt Champion

    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 a QSqlTableModel 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 this

    QSqlTableModel *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 Champion

    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#submitAll

    Or 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


  • Lifetime Qt Champion

    @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.

    https://www.dropbox.com/s/lk0irtwlorat382/mysql2.zip?dl=0



  • I made it workable with your help, can you tell me how to make the topic RESOLVED?


  • Lifetime Qt Champion

    @Ahsan-Niaz

    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:
    alt text

    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.


Log in to reply