Issue
you're help is needed: the mystery is located in Method setupNumRibs.
In my test setup the table contains only one row.
res = self.setData(self.index(0, 1), numRows )
Executes and does the table update as expected.
res = self.setData(self.index(1, 1), numRows )
Must fail, as the row to be updated does not exist. res is FALS as expected, BUT on the cmd line I get:
model ErrT : 0
model Err text : ||
db ErrT : 0
db Err text : ||
Somwhow the
modelError = self.lastError()
Fails, I don't get the correct info back, but I don't see WHY :-(
The full code:
from Singleton.Singleton import Singleton
class ProcessorModel(QSqlTableModel, metaclass=Singleton):
def __init__(self, parent=None):
self.fileNamePath = ''
self.fileVersion = ''
# open database
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("processorModel.sqlite")
if not self.db.open():
logging.error(self.__className+ '.__init__ cannot open db')
super().__init__()
# make sure tables are there
self.rib_M = self.RibModel()
self.wing_M = self.WingModel()
def isValid( self, fileName ):
def setFileName( self, fileName ):
def openFile(self):
def readFile(self):
def remTabSpace(self, line):
def remTabSpaceQuot(self, line):
class WingModel(QSqlTableModel, metaclass=Singleton):
def createWingTable(self):
def __init__(self, parent=None):
def syncData(self, q):
class RibModel(QSqlTableModel, metaclass=Singleton):
__className = 'RibModel'
RibNumCol = 1
xribCol = 2
yLECol = 3
yTECol = 4
xpCol = 5
zCol = 6
betaCol = 7
RPCol = 8
WashinCol = 9
def createRibTable(self):
logging.debug(self.__className+'.createRibTable')
query = QSqlQuery()
query.exec("DROP TABLE if exists Rib;")
query.exec("create table if not exists Rib ("
"ID INT PRIMARY KEY,"
"RibNum varchar(50),"
"xrib varchar(50),"
"yLE varchar(50),"
"yTE varchar(50),"
"xp varchar(50),"
"z varchar(50),"
"beta varchar(50),"
"RP varchar(50),"
"Washin varchar(50));")
query.exec("INSERT into Rib (ID) Values( '1' );")
def __init__(self, parent=None):
'''
:method: Constructor
'''
super().__init__()
self.createRibTable()
self.setTable("Rib")
self.select()
self.setEditStrategy(QSqlTableModel.OnFieldChange)
def setupNumRibs(self, halfNumRibs):
logging.debug(self.__className+'.setupNumRibs')
numRows = self.rowCount()
res = self.setData(self.index(1, 1), numRows )
if not res:
modelError = self.lastError()
print('model ErrT : %s' %modelError.type())
print('model Err text : |%s|' %modelError.text())
print()
procM = ProcessorModel()
dbError = procM.db.lastError()
print('db ErrT : %s' %dbError.type())
print('db Err text : |%s|' %dbError.text())
print()
The Singleton Class can be found already here
Solution
There is no mystery if you read the docs:
QSqlError QSqlQueryModel::lastError() const
Returns information about the last error that occurred on the database.
It is clearly indicated that lastError() indicates an error that happens when interacting with the database, but in your case it does not interact since the model first verifies that the QModelIndex is valid, and since yours is not valid then it returns immediately false without doing any transaction with the database.
On the other hand, don't complicate your problem by adding Singleton or other elements that are just noise. It would be recommended that for each question you create an MRE focused only on the problem and thus save us time to help you instead of having to eliminate silly code.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.