Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. InsertRow
Qt 6.12 LTS is out! See what's new in the release blog

InsertRow

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 5 Posters 9.5k 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.
  • D Offline
    D Offline
    Duy Khang
    wrote on last edited by
    #11

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

    JonBJ 1 Reply Last reply
    0
    • D Duy Khang

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

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

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

        JonBJ 1 Reply Last reply
        0
        • D Duy Khang

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          2
          • D Offline
            D Offline
            Duy Khang
            wrote on last edited by
            #15

            Thank you very much

            1 Reply Last reply
            0
            • JonBJ JonB

              @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 last edited by
              #16

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

              JonBJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                3
                • D Duy Khang

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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 last edited by
                    #19

                    @Christian-Ehrlicher thank you.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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 last edited by
                      #20

                      @JonB yes, thank you, it works

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @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 last edited by
                        #21

                        @JonB hi,
                        set.png

                        how can i change the name of column in picture ?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #22

                          What names do you want there ?

                          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 last edited by Duy Khang
                            #23

                            @SGaist hi, how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #24

                              The same as usual.

                              As for setting the header data, it's shown in the QSqlTableModel documentation.

                              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
                              1
                              • D Offline
                                D Offline
                                Duy Khang
                                wrote on last edited by
                                #25

                                yes, i have done it and then how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.

                                JonBJ 1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #26

                                  What issue do you have with using insertRow ?

                                  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 Duy Khang

                                    yes, i have done it and then how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #27

                                    @Duy-Khang said in InsertRow:

                                    how can I insert a row after the current row, assuming we don't have any rows at first

                                    In addition to what @SGaist has been saying, how do you have a "current" row to insert after if you don't have any rows to start with?

                                    1 Reply Last reply
                                    1
                                    • A Offline
                                      A Offline
                                      ArthurPYQT
                                      wrote on last edited by
                                      #28
                                      import sys
                                      
                                      from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton
                                      from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord
                                      
                                      class demo(QWidget):
                                          def __init__(self):
                                              super().__init__()
                                              lyt = QVBoxLayout()
                                              self.setLayout(lyt)
                                              tview = QTableView()
                                              btn = QPushButton('add')
                                              lyt.addWidget(tview)
                                              lyt.addWidget(btn)
                                              db = QSqlDatabase.addDatabase("QSQLITE")
                                              db.setDatabaseName(r"xxx.db")
                                              self.model = testModel(db)
                                              tview.setModel(self.model)
                                              btn.clicked.connect(self.insertNewRecord)
                                          
                                          def insertNewRecord(self):
                                              r = QSqlRecord()
                                              r.setValue('name', 'hello')
                                              r.setValue('age', 23)
                                      
                                              if self.model.insertRecord(0, r):
                                      
                                                  print(self.model.submitAll())
                                                  print(self.model.lastError().text())
                                              else:
                                                  self.model.db.rollback()
                                                  print(self.model.lastError().text())
                                              self.model.select()
                                              
                                      class testModel(QSqlTableModel):
                                          def __init__(self, db):
                                              super().__init__()
                                              self.db = db
                                           
                                              print(self.db.open())
                                              self.setTable('test')
                                              self.setEditStrategy(QSqlTableModel.OnManualSubmit)
                                              print(self.select())
                                              print(self.lastError().text())
                                              print(self.tableName())
                                      
                                      if __name__ == '__main__':
                                          app = QApplication(sys.argv)
                                          t = demo()
                                          t.show()
                                          sys.exit(app.exec())
                                      
                                      

                                      data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.

                                      how to insert a record into the table?

                                      JonBJ 1 Reply Last reply
                                      0
                                      • A ArthurPYQT
                                        import sys
                                        
                                        from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton
                                        from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord
                                        
                                        class demo(QWidget):
                                            def __init__(self):
                                                super().__init__()
                                                lyt = QVBoxLayout()
                                                self.setLayout(lyt)
                                                tview = QTableView()
                                                btn = QPushButton('add')
                                                lyt.addWidget(tview)
                                                lyt.addWidget(btn)
                                                db = QSqlDatabase.addDatabase("QSQLITE")
                                                db.setDatabaseName(r"xxx.db")
                                                self.model = testModel(db)
                                                tview.setModel(self.model)
                                                btn.clicked.connect(self.insertNewRecord)
                                            
                                            def insertNewRecord(self):
                                                r = QSqlRecord()
                                                r.setValue('name', 'hello')
                                                r.setValue('age', 23)
                                        
                                                if self.model.insertRecord(0, r):
                                        
                                                    print(self.model.submitAll())
                                                    print(self.model.lastError().text())
                                                else:
                                                    self.model.db.rollback()
                                                    print(self.model.lastError().text())
                                                self.model.select()
                                                
                                        class testModel(QSqlTableModel):
                                            def __init__(self, db):
                                                super().__init__()
                                                self.db = db
                                             
                                                print(self.db.open())
                                                self.setTable('test')
                                                self.setEditStrategy(QSqlTableModel.OnManualSubmit)
                                                print(self.select())
                                                print(self.lastError().text())
                                                print(self.tableName())
                                        
                                        if __name__ == '__main__':
                                            app = QApplication(sys.argv)
                                            t = demo()
                                            t.show()
                                            sys.exit(app.exec())
                                        
                                        

                                        data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.

                                        how to insert a record into the table?

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #29

                                        @ArthurPYQT
                                        Since you call print(self.model.lastError().text()) both when insertRecord(0, r) succeeds and when it fails, do you want to tell us (actually prove) which route your code is following...?

                                        A 1 Reply Last reply
                                        1
                                        • JonBJ JonB

                                          @ArthurPYQT
                                          Since you call print(self.model.lastError().text()) both when insertRecord(0, r) succeeds and when it fails, do you want to tell us (actually prove) which route your code is following...?

                                          A Offline
                                          A Offline
                                          ArthurPYQT
                                          wrote on last edited by
                                          #30

                                          @JonB self.model.insertRecord(0, r) returns True, self.model.submitAll() returns False, self.model.lastError().text() returns "No fields to update"

                                          A 1 Reply Last reply
                                          0

                                          • Login

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