Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MVC Pattern (Custom QSqlQueryModel) - Relay a DB Error message to User
Forum Update on Monday, May 27th 2025

MVC Pattern (Custom QSqlQueryModel) - Relay a DB Error message to User

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 344 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rhdr
    wrote on 23 Oct 2019, 09:33 last edited by Rhdr
    #1

    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?
    J 1 Reply Last reply 23 Oct 2019, 10:03
    0
    • R Rhdr
      23 Oct 2019, 10:09

      @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?

      J Offline
      J Offline
      JonB
      wrote on 23 Oct 2019, 10:19 last edited by JonB
      #4

      @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?

      R 1 Reply Last reply 23 Oct 2019, 10:32
      1
      • R Rhdr
        23 Oct 2019, 09:33

        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?
        J Offline
        J Offline
        JonB
        wrote on 23 Oct 2019, 10:03 last edited by JonB
        #2

        @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?

        R 1 Reply Last reply 23 Oct 2019, 10:09
        0
        • J JonB
          23 Oct 2019, 10:03

          @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?

          R Offline
          R Offline
          Rhdr
          wrote on 23 Oct 2019, 10:09 last edited by Rhdr
          #3

          @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?

          J 1 Reply Last reply 23 Oct 2019, 10:19
          0
          • R Rhdr
            23 Oct 2019, 10:09

            @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?

            J Offline
            J Offline
            JonB
            wrote on 23 Oct 2019, 10:19 last edited by JonB
            #4

            @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?

            R 1 Reply Last reply 23 Oct 2019, 10:32
            1
            • J JonB
              23 Oct 2019, 10:19

              @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?

              R Offline
              R Offline
              Rhdr
              wrote on 23 Oct 2019, 10:32 last edited by
              #5

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

              1 Reply Last reply
              0

              1/5

              23 Oct 2019, 09:33

              • Login

              • Login or register to search.
              1 out of 5
              • First post
                1/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved