Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    1. You can directly create a QMessageBox from within the remove rows function but then you violate MVC
    2. Signals & slots may be used... might just be me but it feels like a poor fit for the situation
    3. 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#lastError

    Returns 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 own detailedLastError() 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 your removeRows() 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?



  • @JonB Sorry for the confusion I am implementing the MV without the C. (Trying to comply to the Qt way of doing things)
    So the best/preferred way would be to use #2 Signals & Slots?



  • @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#lastError

    Returns 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 own detailedLastError() on a similar principle.

    What do you think? Does this make sense and work for you?



  • @JonB thank you, it makes perfect sense. (don't know what I was thinking lol)


Log in to reply