0

Recently switched to Pyqt6 databases and can't figure out the value of this enum type:

model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)

I've tried:

  • Passing in the numerical value of the enum type ie 0. The interpreter complains saying that it can't accept int types.

Has anyone figured what the new value should be? Is there a way I or anyone else can find out for themselves?

from PyQt6.QtWidgets import QMessageBox, QApplication, QTableView, QVBoxLayout, QPushButton, QWidget
from PyQt6.QtSql import QSqlDatabase, QSqlTableModel
from PyQt6 import QtCore
from PyQt6 import QtSql

def initializeModel(model):
    model.setTable('authors1')
    model.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
    model.select()
    model.setHeaderData(1, QtCore.Qt.Orientations.Horizontal, 'Author name(s)')

def createView(model):
   view = QTableView()
   view.setModel(model)   
   view.resize(230, 254)
   view.hideColumn(0)
   view.setColumnWidth(1, 200)
   return view

def addRecord():
    sqm.insertRow(sqm.rowCount())

def delRecord() :    
    sqm.removeRow(view1.currentIndex().row())
    sqm.select()
user1801060
  • 2,733
  • 6
  • 25
  • 44
  • use `QSqlTableModel.EditStrategy.OnFieldChange`. See the docs: https://doc.qt.io/qt-5/qsqltablemodel.html#EditStrategy-enum – eyllanesc May 24 '21 at 08:50
  • @eyllanesc: With all due respect, that is what I've done. For UI, the enum types are pretty easy to figure out, but this may be a bug in Pyqt6. Please look at my code again to see what I'm saying! I can post more code if that would help. – user1801060 May 24 '21 at 09:11
  • Do you realize that in my comment there is the term `X.EditStrategy.Y`? in your current code use `model.setEditStrategy(QSqlTableModel.EditStrategy.OnFieldChange)` – eyllanesc May 24 '21 at 09:16
  • That is not a bug but PyQt6 (also PySide6) are being more strict since now they implement python Enums with all their rules. Please read my comments in more detail. – eyllanesc May 24 '21 at 09:18
  • @eyllanesc: I skipped the enum and got my code to work by using: "model.setEditStrategy == 2" I'll take a long break and then review your answer again. – user1801060 May 24 '21 at 09:20

0 Answers0