How to get QStandardItemModel error message
-
I have inherited 32K lines of code with insufficient error checking, which I am trying to rectify. Using Qt 5.7. (I am using PyQt, not that that matters.)
Given that Qt's way of doing things is to return a "false" result rather than throwing an error, I have learnt, for example, that given a
QSqlTableModel
ifQSqlTableModel.select()
returnsFalse
I can callQSqlQueryModel.lastError()
to find out/report why.Now I turn to
QStandardItemModel
. Given code like:model = QStandardItemModel(0, columnCount, self) success = model.setData(index, value) if not success: # Where is the error reason?
There are many possibilities why this might error, e.g. column out of range, unacceptable value. The docs for
QStandardItemModel::setData()
just say it is reimplemented fromQAbstractItemModel::setData()
, and neither one of them has a word to say about error information/reason? (This may also apply to other Qt functions.)Thank you.
-
Looking at the source, actually, the only possible way of it failing is
index
being invalid -
@VRonin said in How to get QStandardItemModel error message:
Looking at the source, actually, the only possible way of it failing is
index
being invalidIn that particular case that may be so. But the question is generic: there seem to be many places I see where I need to test a Qt call for success/failure, but also many places where I do not see any way at getting at the reason for a failure.
I was expecting a
lastError()
-type function, but it's beginning to look like that facility applies to, say,QSqlTableModel
but notQStandardItemModel
.I come from a background where library calls tend to throw an exception (with some detail in it) rather than returning false, and then relying on some other means to get the reason. Or, Windows has
GetLastError()
after a system call failure. I guess it just isn't like that with Qt. Shame! -
Functions as
QSqlQuery::lastError()
are not meant for the developer, they are designed to allow the end users to receive the details of the message.Debugging details, like you are trying to get, are handled by stepping through your debugger, there is no need for Qt to pretty print what went wrong as it would be unnecessary, performance deteriorating, overhead
-
@VRonin said in How to get QStandardItemModel error message:
Functions as
QSqlQuery::lastError()
are not meant for the developer, they are designed to allow the end users to receive the details of the message.Debugging details, like you are trying to get, are handled by stepping through your debugger, there is no need for Qt to pretty print what went wrong as it would be unnecessary, performance deteriorating, overhead
Pardon? I am not talking about debugging, particularly (though it is relevant there too). These are not "debugging details", they are potential runtime errors which may occur and I wish to report to the user at runtime. Nor am I talking about Qt doing any printing of anything: I want to retrieve the error reason so that I can (choose to) report it to the user.
With, say,
QSqlTableModel
, since it does not throw exceptions I must put in code to check return results from any function where appropriate, e.g.:model = QSqlTableModel() success = model.select() if not success: raise model.lastError().text()
(Existing code was not doing this, it was getting errors and they went unnoticed and unreported.) Hence I was looking for an equivalent for
QStandardModelItem
, as per the example in my original post, but it seems Qt does not offer anything like this.... -
@JNBarchan QStandardItemModel uses QVariant as data. You can set and get any QVariant compatible data. There's no reason to have error handling because the class isn't meant for error prone data. See QStandardItem and QVariant for details. QSqlTableModel.lastError is specific to SQL.
If you want to, you can implement your own runtime error handling in your model. QStandardItemModel is mostly meant for simple cases, but you can subclass it as the docs say: "Reimplement data() and setData() if you want to perform custom handling of data queries and/or control how an item's data is represented." Just add your own error handling mechanism. Or inherit from QAbstractItemModel if performance and low-level details are important. QSqlTableModel does that, adding SQL specific things, one of which is SQL error handling.