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. setEditStrategy AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'
Forum Updated to NodeBB v4.3 + New Features

setEditStrategy AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 3 Posters 1.2k 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.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on last edited by PythonQTMarlem
    #1

    Hello,
    There are many examples here in the forum that it works like this:

            filename = os.path.join(os.path.dirname(__file__), "telefonnummern_verwaltung.db")
            db = QSqlDatabase.addDatabase('QSQLITE')
            db.setDatabaseName(filename)
            if db.open():
                self.table_model = QSqlTableModel()
                self.table_model.setTable("telefon_verwaltung")
                self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
                self.table_model.select()
    
    

    But I get this error message:

    self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
    AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'

    What am I doing wrong?

    JonBJ 1 Reply Last reply
    0
    • P PythonQTMarlem

      Hello,
      There are many examples here in the forum that it works like this:

              filename = os.path.join(os.path.dirname(__file__), "telefonnummern_verwaltung.db")
              db = QSqlDatabase.addDatabase('QSQLITE')
              db.setDatabaseName(filename)
              if db.open():
                  self.table_model = QSqlTableModel()
                  self.table_model.setTable("telefon_verwaltung")
                  self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
                  self.table_model.select()
      
      

      But I get this error message:

      self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
      AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'

      What am I doing wrong?

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

      @PythonQTMarlem
      Goodness knows, it looks correct to me, this is Python/PySide.... Can you maybe press F2 key in Creator on QSqlTableModel and it takes you to wherever the PySide class definition is so you can look around to see if OnFieldChange is there?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on last edited by
        #3

        @JonB Thank you for your answer.
        I don't use the qt-creator. I code qt in PyCharm.

        In my imports I have just PyQt6. No PySide.

        can I also implement your idea without qt Creator?

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

          Hi,

          Can you try with QSqlTableModel.EditStrategy.OnFieldChange ?

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

          P 1 Reply Last reply
          0
          • P PythonQTMarlem

            @JonB Thank you for your answer.
            I don't use the qt-creator. I code qt in PyCharm.

            In my imports I have just PyQt6. No PySide.

            can I also implement your idea without qt Creator?

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

            @PythonQTMarlem
            Well whatever the key is in PyCharm then. To let you browse definitions.
            Try @SGaist's suggestion.

            P 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Can you try with QSqlTableModel.EditStrategy.OnFieldChange ?

              P Offline
              P Offline
              PythonQTMarlem
              wrote on last edited by PythonQTMarlem
              #6

              @SGaist Thank you.
              Your code doesn't produce an error message.
              This code also does not generate an error message:

              QSqlTableModel.EditStrategy.OnFieldChange
              

              or this Code:

              self.table_model.EditStrategy.OnFieldChange
              

              But when I change a column in a record. I then go to another column in the same data set and then exit the program. Then my change was not saved.

              When I change the record, then my change was saved.

              1 Reply Last reply
              0
              • JonBJ JonB

                @PythonQTMarlem
                Well whatever the key is in PyCharm then. To let you browse definitions.
                Try @SGaist's suggestion.

                P Offline
                P Offline
                PythonQTMarlem
                wrote on last edited by
                #7

                @JonB
                Thanks. I just did. Unfortunately didn't quite have the desired result.

                JonBJ 1 Reply Last reply
                0
                • P PythonQTMarlem

                  @JonB
                  Thanks. I just did. Unfortunately didn't quite have the desired result.

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

                  @PythonQTMarlem
                  Well at least that has resolved the Python issue, thanks to @SGaist's correct guess.

                  Try with OnManualSubmit and an explicit submitAll() (e.g. from a button, and check its return result). Find out whether it's actually OnFieldSubmit or updating in general which is at issue.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    PythonQTMarlem
                    wrote on last edited by
                    #9

                    I have the solution:
                    self.table_model.setEditStrategy(QSqlTableModel.EditStrategy.OnFieldChange)

                    look here:
                    https://www.tutorialspoint.com/pyqt/pyqt_database_handling.htm

                    Thank you for your help!

                    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