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 Updated to NodeBB v4.3 + New Features

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 1 Watching
  • 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 Ahsan Niaz

    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

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #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 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
      JonBJ 1 Reply Last reply
      0
      • A Ahsan Niaz

        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
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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 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?

          mrjjM 1 Reply Last reply
          0
          • A Ahsan Niaz

            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?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #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 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

              mrjjM 1 Reply Last reply
              0
              • A Ahsan Niaz

                @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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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 last edited by
                  #10

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

                  mrjjM 1 Reply Last reply
                  1
                  • A Ahsan Niaz

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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #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 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

                      • Login

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