Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make TableView Editable in QT Designer form class without buttons
Forum Update on Monday, May 27th 2025

Make TableView Editable in QT Designer form class without buttons

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ahsan Niaz
    wrote on 9 Jan 2021, 09:55 last edited by Ahsan Niaz 1 Sept 2021, 10:21
    #1

    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

    J 1 Reply Last reply 9 Jan 2021, 10:13
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Jan 2021, 10:11 last edited by
      #2

      You should take a look at QSqlTableModel

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • A Ahsan Niaz
        9 Jan 2021, 09:55

        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

        J Offline
        J Offline
        JonB
        wrote on 9 Jan 2021, 10:13 last edited by JonB 1 Sept 2021, 10:16
        #3

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

        1 Reply Last reply
        2
        • A Offline
          A Offline
          Ahsan Niaz
          wrote on 9 Jan 2021, 10:16 last edited by
          #4

          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
          J 1 Reply Last reply 9 Jan 2021, 10:21
          0
          • A Ahsan Niaz
            9 Jan 2021, 10:16

            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
            J Offline
            J Offline
            JonB
            wrote on 9 Jan 2021, 10:21 last edited by JonB 1 Sept 2021, 10:21
            #5

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

            1 Reply Last reply
            1
            • A Offline
              A Offline
              Ahsan Niaz
              wrote on 9 Jan 2021, 10:34 last edited by
              #6

              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?

              M 1 Reply Last reply 9 Jan 2021, 10:39
              0
              • A Ahsan Niaz
                9 Jan 2021, 10:34

                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?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 9 Jan 2021, 10:39 last edited by mrjj 1 Sept 2021, 10:40
                #7

                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

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  Ahsan Niaz
                  wrote on 9 Jan 2021, 10:49 last edited by
                  #8

                  @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

                  M 1 Reply Last reply 9 Jan 2021, 11:03
                  0
                  • A Ahsan Niaz
                    9 Jan 2021, 10:49

                    @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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 9 Jan 2021, 11:03 last edited by
                    #9

                    @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

                    1 Reply Last reply
                    2
                    • A Offline
                      A Offline
                      Ahsan Niaz
                      wrote on 9 Jan 2021, 11:21 last edited by
                      #10

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

                      M 1 Reply Last reply 9 Jan 2021, 11:28
                      1
                      • A Ahsan Niaz
                        9 Jan 2021, 11:21

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

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 9 Jan 2021, 11:28 last edited by mrjj 1 Sept 2021, 11:30
                        #11

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Ahsan Niaz
                          wrote on 9 Jan 2021, 11:31 last edited by
                          #12

                          i had a form where i had to click (Load Table) . i hide that form to make the tablewindow editable.

                          1 Reply Last reply
                          1

                          1/12

                          9 Jan 2021, 09:55

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved