MVC Pattern (Custom QSqlQueryModel) - Relay a DB Error message to User
-
Q: How do you relay a specific error generated by the database (ex: Sql Server) via the model to the end user in a (Qt)mvc design pattern friendly way?
Ex: Take the model's removeRows() function for example that returns a bool value only. If you build the delete query in the remove rows function, any error message returned from the db is stuck in this model function. The view/controller will only receive the bool value and not any detailed error message.
Possible Solutions:
- You can directly create a QMessageBox from within the remove rows function but then you violate MVC
- Signals & slots may be used... might just be me but it feels like a poor fit for the situation
- Change the removeRows() function to return multiple values (ex: by creating a custom message class or returning a list) - Is this allowed or will it break Qt?
-
@Rhdr
OK.I (could be wrong but I) don't actually think a signal/slot is right here. If something calls
removeRows()
, it's a function which returns a true/false result. You'll have it return false, and then there will be some separate signal arriving (before or afterward), you won't be able to "tie" the signal to the particular function call which failed.For
QSqlQueryModel
you already have https://doc.qt.io/qt-5/qsqlquerymodel.html#lastErrorReturns information about the last error that occurred on the database.
I would leverage that. Your view is allowed to access the model (via
model()
). So after any call the view makes to the model which fails, I think you can call that directly. If you want more than one message, or more detail, or whatever, your model subclass can build its owndetailedLastError()
on a similar principle.What do you think? Does this make sense and work for you?
-
@Rhdr
#1 looks wrong, and for #3 I think yourremoveRows()
must/should remain as the one you are overriding.When you say "MVC Pattern" and "in a (Qt)mvc design pattern friendly way", Qt actually implements a MV-only pattern and removes the Controller. Are you actually using (your own) Controller, or are you just implementing MV without the C?
-
@Rhdr
OK.I (could be wrong but I) don't actually think a signal/slot is right here. If something calls
removeRows()
, it's a function which returns a true/false result. You'll have it return false, and then there will be some separate signal arriving (before or afterward), you won't be able to "tie" the signal to the particular function call which failed.For
QSqlQueryModel
you already have https://doc.qt.io/qt-5/qsqlquerymodel.html#lastErrorReturns information about the last error that occurred on the database.
I would leverage that. Your view is allowed to access the model (via
model()
). So after any call the view makes to the model which fails, I think you can call that directly. If you want more than one message, or more detail, or whatever, your model subclass can build its owndetailedLastError()
on a similar principle.What do you think? Does this make sense and work for you?