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

InsertRow

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 5 Posters 4.3k 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.
  • D Offline
    D Offline
    Duy Khang
    wrote on 17 May 2021, 18:43 last edited by
    #1

    How to insert a new row and new data to every cell of new row ? And how beginInsertRow() work ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 May 2021, 18:44 last edited by
      #2

      Hi,

      What kind of model are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Duy Khang
        wrote on 18 May 2021, 02:34 last edited by Duy Khang
        #3

        i'm using tableView
        i used ui->tableView->model()->insertRow(0), but it does not work !

        J 1 Reply Last reply 18 May 2021, 07:33
        0
        • D Duy Khang
          18 May 2021, 02:34

          i'm using tableView
          i used ui->tableView->model()->insertRow(0), but it does not work !

          J Offline
          J Offline
          JonB
          wrote on 18 May 2021, 07:33 last edited by JonB
          #4

          @Duy-Khang
          @SGaist asked you what model you are using. Not the QTableView. What is your ui->tableView->model()? Because insertRow(0) inserts a row into the model, and if you say "but it does not work !" then you have an issue in your model. Perhaps because you have your own model and have not implemented inserting rows correctly/at all?

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Duy Khang
            wrote on 18 May 2021, 07:49 last edited by
            #5

            i don't know what is model and you can show me how to insert a new row and add data to every cell of new row ? I just want this but i don't know how to do it.

            J 1 Reply Last reply 18 May 2021, 07:52
            0
            • D Duy Khang
              18 May 2021, 07:49

              i don't know what is model and you can show me how to insert a new row and add data to every cell of new row ? I just want this but i don't know how to do it.

              J Offline
              J Offline
              JonB
              wrote on 18 May 2021, 07:52 last edited by JonB
              #6

              @Duy-Khang
              Nobody can without knowing what model you are using. Only you know that, and you really should know what your model is. Have you tried reading the Model/View Tutorial?

              1 Reply Last reply
              1
              • D Offline
                D Offline
                Duy Khang
                wrote on 18 May 2021, 08:01 last edited by
                #7

                newrow.png

                this is my tableView and i want when users click "Đăng Kí"''s pushButton, it will insert a new row

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 18 May 2021, 08:07 last edited by SGaist
                  #8

                  We all understood what you want to do.

                  What we are asking you is what type of model is set on that QTableView ?

                  QSqlTableModel ?
                  QSqlQueryModel ?
                  QStandardItemModel ?
                  Custom QAbstractTableModel based model ?
                  Other ? (to be defined)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    Duy Khang
                    wrote on 18 May 2021, 08:11 last edited by
                    #9

                    it is QSqlQueryModel.

                    J 1 Reply Last reply 18 May 2021, 08:13
                    0
                    • D Duy Khang
                      18 May 2021, 08:11

                      it is QSqlQueryModel.

                      J Offline
                      J Offline
                      JonB
                      wrote on 18 May 2021, 08:13 last edited by JonB
                      #10

                      @Duy-Khang
                      At last! A QSqlQueryModel is for querying SQL, it does not allow insertions, because it does not know where to insert any rows. It sounds like you should switch to a QSqlTableModel for inserts etc.? I assume the rows you show are all in one table, which is where you want to insert/update/delete rows.

                      1 Reply Last reply
                      2
                      • D Offline
                        D Offline
                        Duy Khang
                        wrote on 18 May 2021, 08:22 last edited by
                        #11

                        oh,I don't know that before and how to insert new row if i use QSqlTableModel ?

                        J 1 Reply Last reply 18 May 2021, 08:27
                        0
                        • D Duy Khang
                          18 May 2021, 08:22

                          oh,I don't know that before and how to insert new row if i use QSqlTableModel ?

                          J Offline
                          J Offline
                          JonB
                          wrote on 18 May 2021, 08:27 last edited by JonB
                          #12

                          @Duy-Khang
                          The code you showed would work if the model is a QSqlTableModel instead of a QSqlQueryModel, and you have specified the table. You can search https://doc.qt.io/qt-5/qsqltablemodel.html and https://doc.qt.io/qt-5/qsqltablemodel-members.html for insertRow and see what you come across. There is also QSqlTableModel::insertRecord() etc. if you prefer a record-based interface to a row-based interface, that is a matter of preference.

                          BTW you don't need to worry about/implement the beginInsertRows() etc. you asked about if you use a QSqlTableModel, its implementation handles that for you.

                          1 Reply Last reply
                          2
                          • D Offline
                            D Offline
                            Duy Khang
                            wrote on 18 May 2021, 08:32 last edited by
                            #13

                            and QSqlTableModel can not setQuery() how to do this ?

                            J 1 Reply Last reply 18 May 2021, 08:41
                            0
                            • D Duy Khang
                              18 May 2021, 08:32

                              and QSqlTableModel can not setQuery() how to do this ?

                              J Offline
                              J Offline
                              JonB
                              wrote on 18 May 2021, 08:41 last edited by
                              #14

                              @Duy-Khang
                              Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details.

                              D 1 Reply Last reply 18 May 2021, 17:56
                              2
                              • D Offline
                                D Offline
                                Duy Khang
                                wrote on 18 May 2021, 08:43 last edited by
                                #15

                                Thank you very much

                                1 Reply Last reply
                                0
                                • J JonB
                                  18 May 2021, 08:41

                                  @Duy-Khang
                                  Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details.

                                  D Offline
                                  D Offline
                                  Duy Khang
                                  wrote on 18 May 2021, 17:56 last edited by
                                  #16

                                  @JonB
                                  newrow.png
                                  do you know how to disable or turn off the these indexs in this picture ?

                                  J 1 Reply Last reply 18 May 2021, 17:58
                                  0
                                  • Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 18 May 2021, 17:58 last edited by
                                    #17

                                    @Duy-Khang said in InsertRow:

                                    do you know how to disable or turn off the these indexs in this picture ?

                                    Hide the horizontal and vertical header.

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

                                    D 1 Reply Last reply 18 May 2021, 18:09
                                    3
                                    • D Duy Khang
                                      18 May 2021, 17:56

                                      @JonB
                                      newrow.png
                                      do you know how to disable or turn off the these indexs in this picture ?

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 18 May 2021, 17:58 last edited by JonB
                                      #18

                                      @Duy-Khang
                                      If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.

                                      Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!

                                      In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.

                                      D 2 Replies Last reply 18 May 2021, 18:21
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher
                                        18 May 2021, 17:58

                                        @Duy-Khang said in InsertRow:

                                        do you know how to disable or turn off the these indexs in this picture ?

                                        Hide the horizontal and vertical header.

                                        D Offline
                                        D Offline
                                        Duy Khang
                                        wrote on 18 May 2021, 18:09 last edited by
                                        #19

                                        @Christian-Ehrlicher thank you.

                                        1 Reply Last reply
                                        0
                                        • J JonB
                                          18 May 2021, 17:58

                                          @Duy-Khang
                                          If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.

                                          Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!

                                          In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.

                                          D Offline
                                          D Offline
                                          Duy Khang
                                          wrote on 18 May 2021, 18:21 last edited by
                                          #20

                                          @JonB yes, thank you, it works

                                          1 Reply Last reply
                                          0

                                          1/32

                                          17 May 2021, 18:43

                                          • Login

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