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

Put data in TableView after SQl query

Scheduled Pinned Locked Moved Qt for Python
29 Posts 4 Posters 3.7k 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 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
                    • Karoluss96K Karoluss96

                      OK, now I have at beginning:

                      q= QSqlQueryModel()
                        table = QSqlTableModel()
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #22

                      @Karoluss96
                      Why do you have both a QSqlQueryModel and a QSqlTableModel? Why two models?

                      And you are assigning each of these to a function-local variable now. You will not be able to access these outside of this function, and they may be destroyed when they go out of scope.

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

                        Both I catch in one funtion, so I needn't put them global.
                        Now I have that:

                         q= QSqlQueryModel()
                                table = QSqlTableModel()
                                self.dlg.tableView_3.show()
                                if con == Zest[0]:
                                   sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"
                                   q=query.exec_(sql2)
                                   a=table.setQuery(q)
                                   self.dlg.tableView_5.setModel(a)  
                                   print (a)
                        

                        with an error: TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'bool'

                        jsulmJ 1 Reply Last reply
                        0
                        • Karoluss96K Karoluss96

                          Both I catch in one funtion, so I needn't put them global.
                          Now I have that:

                           q= QSqlQueryModel()
                                  table = QSqlTableModel()
                                  self.dlg.tableView_3.show()
                                  if con == Zest[0]:
                                     sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"
                                     q=query.exec_(sql2)
                                     a=table.setQuery(q)
                                     self.dlg.tableView_5.setModel(a)  
                                     print (a)
                          

                          with an error: TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'bool'

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

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

                          q= QSqlQueryModel()
                          table = QSqlTableModel()

                          I'm wondering how often we need to write that you are creating local variables which are destroyed as soon as the method finishes?

                          And did you actually read what @JonB wrote? Again: why do you have QSqlQueryModel AND QSqlTableModel?

                          Why are you overwriting q here:

                          q=query.exec_(sql2)
                          

                          ?
                          You should really think more about what you are doing...

                          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
                            #25

                            OK, I read his suggestion, but still don't what is a problem in 'creating local variables which are destroyed as soon as the method finishes' - this variables exist only in this function

                            q=query.exec_(sql2) come from different function in my app, which execute query in assigned variable, that have the SQL command as a string.

                            Now the error is little different: TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'str'

                            jsulmJ 1 Reply Last reply
                            0
                            • Karoluss96K Karoluss96

                              OK, I read his suggestion, but still don't what is a problem in 'creating local variables which are destroyed as soon as the method finishes' - this variables exist only in this function

                              q=query.exec_(sql2) come from different function in my app, which execute query in assigned variable, that have the SQL command as a string.

                              Now the error is little different: TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'str'

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

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

                              but still don't what is a problem in 'creating local variables which are destroyed as soon as the method finishes'

                              Simple: your models (q and table) will be destroyed as soon as the method finishes, so your tableView_5 will not show anything.

                              "q=query.exec_(sql2) come from different function in my app" - don't just copy/paste something without thinking about what you are doing.

                              "Now the error is little different" - please, if you get an error show also the code causing this error!

                              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
                                #27

                                Problem is in last line of this code:

                                sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"
                                           q=query.exec_(sql2)
                                           print (q)
                                           c=self.dlg.tableView_5.setModel(q)  
                                

                                I don't know why q is converted from string to bool.
                                If I put in setModel sql2 instead of q it shout that here can be the string

                                about sentance: " models (q and table) will be destroyed as soon as the method finishes, so your tableView_5 will not show anything." what do you propose?

                                jsulmJ 1 Reply Last reply
                                0
                                • Karoluss96K Karoluss96

                                  Problem is in last line of this code:

                                  sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"
                                             q=query.exec_(sql2)
                                             print (q)
                                             c=self.dlg.tableView_5.setModel(q)  
                                  

                                  I don't know why q is converted from string to bool.
                                  If I put in setModel sql2 instead of q it shout that here can be the string

                                  about sentance: " models (q and table) will be destroyed as soon as the method finishes, so your tableView_5 will not show anything." what do you propose?

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

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

                                  I don't know why q is converted from string to bool.

                                  Because you copy pasted code without thinking.
                                  What do you think this line of code is doing?

                                  q=query.exec_(sql2)
                                  

                                  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
                                    #29

                                    It should execute the sql in a different functions eg.

                                          sql9 = 'SELECT * FROM ZARZADZANIE.STATUS_PRZEK_SLO'
                                            query.exec_(sql9)
                                    

                                    It works, but here the result of query (by while query.next():)
                                    goes to ComboBox not TableView

                                    and earlier query is declared global as:

                                    global query
                                    query = QSqlQuery(db)
                                    
                                    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