Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Put data in TableView after SQl query
Qt 6.11 is out! See what's new in the release blog

Put data in TableView after SQl query

Scheduled Pinned Locked Moved Qt for Python
29 Posts 4 Posters 8.5k 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.
  • Karoluss96K Offline
    Karoluss96K Offline
    Karoluss96
    wrote on last edited by
    #2

    I've tried sth by adding:

    q=query.exec_(sql2)
               model.setQuery(q)
               a=self.dlg.tableView_5.setModel(model)
    

    inspiring by this question in forum:
    https://forum.qt.io/topic/102277/how-to-insert-data-to-table-from-another-table-sql-in-qt/8

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NYBL
      wrote on last edited by
      #3

      please add a minimal reproducible example.

      Karoluss96K 1 Reply Last reply
      0
      • N NYBL

        please add a minimal reproducible example.

        Karoluss96K Offline
        Karoluss96K Offline
        Karoluss96
        wrote on last edited by
        #4

        @NYBL You mean by SQL?
        I can't because the database is big and keep sensitive data.
        I make this code for an app for QGIS plug-in, in which every question is make after choosing current option in ComboBox

        JonBJ 1 Reply Last reply
        0
        • Karoluss96K Karoluss96

          @NYBL You mean by SQL?
          I can't because the database is big and keep sensitive data.
          I make this code for an app for QGIS plug-in, in which every question is make after choosing current option in ComboBox

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

          @Karoluss96
          You should not be using QStandard... for records from a SQL database. You should use either QSqlTableModel or QSqlQueryModel for the database query and a QTableView with that as a model. Then it should display your received rows/columns. You will not need to use insertColumn() or appendRow() either. the query should fill the model and the view should display what is in the model.

          1 Reply Last reply
          2
          • Karoluss96K Offline
            Karoluss96K Offline
            Karoluss96
            wrote on last edited by Karoluss96
            #6

            I'm trying by using your option:

             global model
                    q= QSqlQueryModel()
                    model = QTableView()
                    res=QStandardItem()
                    if con == Zest[0]:
                       sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-(
                       query.exec_(sql2)
                       a=q.setQuery(sql2)
                       self.dlg.tableView_5.setModel(a)
                       while query.next():
                           a=res.setQuery(q)
                           b=res.insertColumns(a)
                           model.appendRow(b)
                       self.dlg.tableView_5.show()
                    if con == Zest[1]:       
                        plan22 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA where ROK_PLANOWANIA=2022"
                        query.exec_(plan22)
                        a=q.setQuery(plan22)
                        self.dlg.tableView_5.setModel(a)
                                    print (a)
                                    while query.next():
                                        a=model.setQuery(q)
                                        b=res.insertColumns(a)
                                        model.appendRow(b)
                                    self.dlg.tableView_5.show()
            

            but nothing appears in Table View. Sth should I make wrong?

            jsulmJ 1 Reply Last reply
            0
            • Karoluss96K Karoluss96

              I'm trying by using your option:

               global model
                      q= QSqlQueryModel()
                      model = QTableView()
                      res=QStandardItem()
                      if con == Zest[0]:
                         sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-(
                         query.exec_(sql2)
                         a=q.setQuery(sql2)
                         self.dlg.tableView_5.setModel(a)
                         while query.next():
                             a=res.setQuery(q)
                             b=res.insertColumns(a)
                             model.appendRow(b)
                         self.dlg.tableView_5.show()
                      if con == Zest[1]:       
                          plan22 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA where ROK_PLANOWANIA=2022"
                          query.exec_(plan22)
                          a=q.setQuery(plan22)
                          self.dlg.tableView_5.setModel(a)
                                      print (a)
                                      while query.next():
                                          a=model.setQuery(q)
                                          b=res.insertColumns(a)
                                          model.appendRow(b)
                                      self.dlg.tableView_5.show()
              

              but nothing appears in Table View. Sth should I make wrong?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Karoluss96 You should put more context - where is this code actually located? I guess some method?
              The problem is probably that you are creating local variables which are destroyed as soon as they go out of scope.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Karoluss96K Offline
                Karoluss96K Offline
                Karoluss96
                wrote on last edited by
                #8

                It's a part of function to put data from selected sql in combobox to table View.

                Begins of it looks that:

                 def zestawienia (self): #obsługa zakładki zestawień
                        con= self.dlg.comboBox_6.currentText()
                        global model
                        q= QSqlQueryModel()
                        model = QTableView()
                        res=QStandardItem()
                        if con == Zest[0]:
                ...
                

                It vision is easy- from combo box show SQL's anwser in TableView below
                na forum.PNG

                jsulmJ 1 Reply Last reply
                0
                • Karoluss96K Karoluss96

                  It's a part of function to put data from selected sql in combobox to table View.

                  Begins of it looks that:

                   def zestawienia (self): #obsługa zakładki zestawień
                          con= self.dlg.comboBox_6.currentText()
                          global model
                          q= QSqlQueryModel()
                          model = QTableView()
                          res=QStandardItem()
                          if con == Zest[0]:
                  ...
                  

                  It vision is easy- from combo box show SQL's anwser in TableView below
                  na forum.PNG

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Karoluss96 As I wrote: you are creating local variables, make them class members instead...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Karoluss96K Offline
                    Karoluss96K Offline
                    Karoluss96
                    wrote on last edited by
                    #10

                    You mean by put some variables in definition of function - next to (self) in brackets?
                    Or put eg. ..setquery and insertColumns at the beginning before if loop?

                    jsulmJ 1 Reply Last reply
                    0
                    • Karoluss96K Karoluss96

                      You mean by put some variables in definition of function - next to (self) in brackets?
                      Or put eg. ..setquery and insertColumns at the beginning before if loop?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Karoluss96 For example:

                      self.model = QTableView()
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • Karoluss96K Offline
                        Karoluss96K Offline
                        Karoluss96
                        wrote on last edited by
                        #12

                        I've done:

                         self.q= QSqlQueryModel()
                         self.model = QTableView()
                        if con == Zest[0]:
                                   sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-(
                                   query.exec_(sql2)
                                   a=q.setQuery(sql2)
                                   self.dlg.tableView_5.setModel(a)
                        

                        but i gains error in loof if: local variable 'q' referenced before assignment

                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • Karoluss96K Karoluss96

                          I've done:

                           self.q= QSqlQueryModel()
                           self.model = QTableView()
                          if con == Zest[0]:
                                     sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-(
                                     query.exec_(sql2)
                                     a=q.setQuery(sql2)
                                     self.dlg.tableView_5.setModel(a)
                          

                          but i gains error in loof if: local variable 'q' referenced before assignment

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Karoluss96 said in Put data in TableView after SQl query:

                          local variable 'q' referenced before assignment

                          it is self.q now, not just q...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • Karoluss96K Offline
                            Karoluss96K Offline
                            Karoluss96
                            wrote on last edited by
                            #14

                            so where i have to put

                            self.model = QTableView()
                            

                            as you suggested?

                            jsulmJ 1 Reply Last reply
                            0
                            • Karoluss96K Karoluss96

                              so where i have to put

                              self.model = QTableView()
                              

                              as you suggested?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @Karoluss96 In the code you already posted!

                              def zestawienia (self): #obsługa zakładki zestawień
                                      con= self.dlg.comboBox_6.currentText()
                                      global model
                                      self.q= QSqlQueryModel()
                                      self.model = QTableView()
                                      res=QStandardItem()
                                      if con == Zest[0]:
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • Karoluss96K Karoluss96

                                I've done:

                                 self.q= QSqlQueryModel()
                                 self.model = QTableView()
                                if con == Zest[0]:
                                           sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-(
                                           query.exec_(sql2)
                                           a=q.setQuery(sql2)
                                           self.dlg.tableView_5.setModel(a)
                                

                                but i gains error in loof if: local variable 'q' referenced before assignment

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

                                @Karoluss96
                                You need to understand about things like variable scopes as a part of Python not Qt. Just saying.

                                I see you still have a QStandardItem. I cannot imagine why. As I said, you simply fill your model from SQL and attach a QTableView to it, that's it.

                                And please do not name as model something which is a QTableView, it's incredibly confusing.

                                1 Reply Last reply
                                1
                                • Karoluss96K Offline
                                  Karoluss96K Offline
                                  Karoluss96
                                  wrote on last edited by Karoluss96
                                  #17

                                  I changed assigning QTableView from model to table.
                                  Then I put it into

                                  self.dlg.tableView_5.setModel(table)
                                  

                                  but it makes an error

                                  TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QTableView'

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • Karoluss96K Karoluss96

                                    I changed assigning QTableView from model to table.
                                    Then I put it into

                                    self.dlg.tableView_5.setModel(table)
                                    

                                    but it makes an error

                                    TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QTableView'

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Karoluss96 said in Put data in TableView after SQl query:

                                    but it makes an error

                                    Would be nice if you would tell us what error it is...

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • Karoluss96K Offline
                                      Karoluss96K Offline
                                      Karoluss96
                                      wrote on last edited by Karoluss96
                                      #19

                                      I put the text of error in a previous post /\

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • Karoluss96K Karoluss96

                                        I put the text of error in a previous post /\

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by jsulm
                                        #20

                                        @Karoluss96 The error is quite clear: you can't set QTableView (which is a view) as model...
                                        What @JonB suggested is to use https://doc.qt.io/qt-6/qsqltablemodel.html or https://doc.qt.io/qt-6/qsqlquerymodel.html as model to get data from your SQL database and set that model in self.dlg.tableView_5

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        1
                                        • Karoluss96K Offline
                                          Karoluss96K Offline
                                          Karoluss96
                                          wrote on last edited by
                                          #21

                                          OK, now I have at beginning:

                                          q= QSqlQueryModel()
                                            table = QSqlTableModel()
                                          
                                          JonBJ 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 Extensions
                                          • Unsolved