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. How to popup a message box when QAbstractModel::setData got invalid data?
Forum Update on Monday, May 27th 2025

How to popup a message box when QAbstractModel::setData got invalid data?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 817 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1

    If an excetion thrown in QAbstractModel::setData catched, it's ok.

    Or should it fallback to a default value when invalid data is commited?

    I think a message box is better? Anyway?

    artwawA JonBJ 2 Replies Last reply
    0
    • jronaldJ jronald

      @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

      "Enter" shall not be enabled as long as the input data is invalid.

      How to disable "Enter" conditionally?

      @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

      No need for a message box.

      A message box can tell the user what's wrong, it is helpful.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #17

      @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

      How to disable "Enter" conditionally?

      Tie it to a validator's validity state.

      @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

      A message box can tell the user what's wrong, it is helpful.

      A UI that is explicit with regard to what it expects is better.

      Do you really like to enter information in an application to just to have a message tell you that you have to do it all over again because of various constraints ?
      Constraints that are usually lost as you need to close the dialog to access the UI again ?

      Make things simple and clear:

      • If you need a number in a range use a QSpinBox
      • If you need a line of text with requirements use a QLineEdit with a suitable validator.
        Add an icon beside it with a QToolTip giving the expected input or a label underneath it with that advice.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • jronaldJ jronald

        If an excetion thrown in QAbstractModel::setData catched, it's ok.

        Or should it fallback to a default value when invalid data is commited?

        I think a message box is better? Anyway?

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #2

        @jronald emit a signal, connect to the slot?

        For more information please re-read.

        Kind Regards,
        Artur

        jronaldJ 1 Reply Last reply
        1
        • artwawA artwaw

          @jronald emit a signal, connect to the slot?

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by
          #3

          @artwaw said in How to popup a message box when QAbstractModel::setData got invalid data?:

          emit a signal, connect to the slot?

          After close the message box, how to keep the inline editor alive?

          1 Reply Last reply
          0
          • jronaldJ jronald

            If an excetion thrown in QAbstractModel::setData catched, it's ok.

            Or should it fallback to a default value when invalid data is commited?

            I think a message box is better? Anyway?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @jronald
            Neither of those is correct behaviour. https://doc.qt.io/qt-5/qabstractitemmodel.html#setData is perfectly clear:

            Returns true if successful; otherwise returns false.

            That is what you must implement, no more and no less. Other parts of Qt infrastructure rely on this behaviour.

            If you need to do something to show the user an issue, you should test that result and show a message in the UI side of your code. And this is almost certianly not an area to be emitting signals from, due to the synchronous nature and return result behaviour of setData().

            jronaldJ 1 Reply Last reply
            3
            • JonBJ JonB

              @jronald
              Neither of those is correct behaviour. https://doc.qt.io/qt-5/qabstractitemmodel.html#setData is perfectly clear:

              Returns true if successful; otherwise returns false.

              That is what you must implement, no more and no less. Other parts of Qt infrastructure rely on this behaviour.

              If you need to do something to show the user an issue, you should test that result and show a message in the UI side of your code. And this is almost certianly not an area to be emitting signals from, due to the synchronous nature and return result behaviour of setData().

              jronaldJ Offline
              jronaldJ Offline
              jronald
              wrote on last edited by jronald
              #5

              @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

              Returns true if successful; otherwise returns false.

              For example, a cell is for a string whose length should be from 3 to 10.

              If the user input 2 or 11 chars in the editor and press "Enter", better if a message box popups and tells the user the rule, and when the user closes the mesage box, he/she can continue to edit or cancel editing by pressing "Esc".

              jronaldJ JonBJ 2 Replies Last reply
              0
              • jronaldJ jronald

                @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                Returns true if successful; otherwise returns false.

                For example, a cell is for a string whose length should be from 3 to 10.

                If the user input 2 or 11 chars in the editor and press "Enter", better if a message box popups and tells the user the rule, and when the user closes the mesage box, he/she can continue to edit or cancel editing by pressing "Esc".

                jronaldJ Offline
                jronaldJ Offline
                jronald
                wrote on last edited by jronald
                #6

                @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                Returns true if successful; otherwise returns false.

                For example, a cell is for string whose length should be from 3 to 10.

                If the user input 2 or 11 chars in the editor and press "Enter", better if a message box popups and tells the user the rule, and when the user closes the mesage box, he/she can continue to edit or cancel editing by pressing "Esc".

                Without a message box, a user will not know what's wrong with the input, the input is just ignored silently when setData returns false.

                jsulmJ 1 Reply Last reply
                0
                • jronaldJ jronald

                  @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                  Returns true if successful; otherwise returns false.

                  For example, a cell is for a string whose length should be from 3 to 10.

                  If the user input 2 or 11 chars in the editor and press "Enter", better if a message box popups and tells the user the rule, and when the user closes the mesage box, he/she can continue to edit or cancel editing by pressing "Esc".

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @jronald
                  I have already told you what setData() must do. If you want to act on a false result code and do something in your UI in response then do that. For this kind of issue, however, you should be using Qt validators to achieve.

                  he input is just ignored silently when setData returns false.

                  So do not ignore the return result! It is there precisely so that you can act on it if you wish to :)

                  1 Reply Last reply
                  3
                  • jronaldJ jronald

                    @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                    @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                    Returns true if successful; otherwise returns false.

                    For example, a cell is for string whose length should be from 3 to 10.

                    If the user input 2 or 11 chars in the editor and press "Enter", better if a message box popups and tells the user the rule, and when the user closes the mesage box, he/she can continue to edit or cancel editing by pressing "Esc".

                    Without a message box, a user will not know what's wrong with the input, the input is just ignored silently when setData returns false.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                    For example, a cell is for string whose length should be from 3 to 10.

                    User input validation should b e done in the UI.
                    See https://doc.qt.io/qt-5/qvalidator.html - there are several derived classes.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    jronaldJ 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                      For example, a cell is for string whose length should be from 3 to 10.

                      User input validation should b e done in the UI.
                      See https://doc.qt.io/qt-5/qvalidator.html - there are several derived classes.

                      jronaldJ Offline
                      jronaldJ Offline
                      jronald
                      wrote on last edited by
                      #9

                      @jsulm said in How to popup a message box when QAbstractModel::setData got invalid data?:

                      User input validation should b e done in the UI.

                      Yes, but where to put the code that pops up the message box?

                      artwawA JonBJ 2 Replies Last reply
                      0
                      • jronaldJ jronald

                        @jsulm said in How to popup a message box when QAbstractModel::setData got invalid data?:

                        User input validation should b e done in the UI.

                        Yes, but where to put the code that pops up the message box?

                        artwawA Offline
                        artwawA Offline
                        artwaw
                        wrote on last edited by
                        #10

                        @jronald As I mentioned, independently of how you act on the return value (@JonB and @jsulm raised absolutely valid points) you can just emit a signal (you'd need to define it the class that handles setData()) and connect it to the slot defined wherever it suits you (class holding the window with the view/main class/wherever convenient and possible).

                        For more information please re-read.

                        Kind Regards,
                        Artur

                        jronaldJ 1 Reply Last reply
                        0
                        • artwawA artwaw

                          @jronald As I mentioned, independently of how you act on the return value (@JonB and @jsulm raised absolutely valid points) you can just emit a signal (you'd need to define it the class that handles setData()) and connect it to the slot defined wherever it suits you (class holding the window with the view/main class/wherever convenient and possible).

                          jronaldJ Offline
                          jronaldJ Offline
                          jronald
                          wrote on last edited by
                          #11

                          @artwaw

                          Anyway to implement the procedure as below:

                          1. User input invalid data and press "Enter"
                          2. Message box popups to tells the user what's wrong
                          3. Close the message box, the editor should not be closed and setData has not been called yet, the user can continue the editing before the message box
                          SGaistS 1 Reply Last reply
                          0
                          • jronaldJ jronald

                            @jsulm said in How to popup a message box when QAbstractModel::setData got invalid data?:

                            User input validation should b e done in the UI.

                            Yes, but where to put the code that pops up the message box?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #12

                            @jronald
                            Respectfully, I am going to disagree with my learned colleague @artwaw here. As i said earlier, I do not see this as a signal situation. setData() returns immediately from wheberever it is called from with a return result, and calling code continues immediately after this. We don't know what that might do. At some (potentially) later point a signal arrives stating there was an error. At which point you suddenly show the user a message.

                            Where are you calling the setData() from? Somewhere in the UI? In that case why not get whatever information you need directly (on the next line of code after calling setData()) and display the message there and then?

                            artwawA 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @jronald
                              Respectfully, I am going to disagree with my learned colleague @artwaw here. As i said earlier, I do not see this as a signal situation. setData() returns immediately from wheberever it is called from with a return result, and calling code continues immediately after this. We don't know what that might do. At some (potentially) later point a signal arrives stating there was an error. At which point you suddenly show the user a message.

                              Where are you calling the setData() from? Somewhere in the UI? In that case why not get whatever information you need directly (on the next line of code after calling setData()) and display the message there and then?

                              artwawA Offline
                              artwawA Offline
                              artwaw
                              wrote on last edited by
                              #13

                              @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                              Respectfully, I am going to disagree with my learned colleague @artwaw here.

                              I agree you know better! No doubts in that. But I think I also see the use case the OP tries to achieve.

                              The flow I had in mind was more along the lines:

                              if (!model.setData()) {
                              emit presentUserWithDialog();
                              }
                              

                              This can be implemented together without breaking stuff, doesn't it? Sorry if that was not clear earlier.
                              I might be, of course, wrong - would not be the first time.

                              For more information please re-read.

                              Kind Regards,
                              Artur

                              JonBJ 1 Reply Last reply
                              0
                              • artwawA artwaw

                                @JonB said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                Respectfully, I am going to disagree with my learned colleague @artwaw here.

                                I agree you know better! No doubts in that. But I think I also see the use case the OP tries to achieve.

                                The flow I had in mind was more along the lines:

                                if (!model.setData()) {
                                emit presentUserWithDialog();
                                }
                                

                                This can be implemented together without breaking stuff, doesn't it? Sorry if that was not clear earlier.
                                I might be, of course, wrong - would not be the first time.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #14

                                @artwaw said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                I agree you know better!

                                I would never assume that :)

                                if (!model.setData()) {
                                emit presentUserWithDialog();
                                }
                                

                                OK, this is better. I had understood you to be following @jronald's proposed path where it would the setData() implementation itself which emitted the signal. Now that I see you have the caller checking the result from setData() and doing the emit I am much happier.

                                Whether that caller is then better emitting a signal or (if it is a UI caller) just doing the message itself there & then is a smaller matter.

                                1 Reply Last reply
                                1
                                • jronaldJ jronald

                                  @artwaw

                                  Anyway to implement the procedure as below:

                                  1. User input invalid data and press "Enter"
                                  2. Message box popups to tells the user what's wrong
                                  3. Close the message box, the editor should not be closed and setData has not been called yet, the user can continue the editing before the message box
                                  SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  Hi,

                                  @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                  1. User input invalid data and press "Enter"

                                  "Enter" shall not be enabled as long as the input data is invalid.

                                  No need for a message box.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  jronaldJ 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Hi,

                                    @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                    1. User input invalid data and press "Enter"

                                    "Enter" shall not be enabled as long as the input data is invalid.

                                    No need for a message box.

                                    jronaldJ Offline
                                    jronaldJ Offline
                                    jronald
                                    wrote on last edited by
                                    #16

                                    @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                    "Enter" shall not be enabled as long as the input data is invalid.

                                    How to disable "Enter" conditionally?

                                    @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                    No need for a message box.

                                    A message box can tell the user what's wrong, it is helpful.

                                    SGaistS 1 Reply Last reply
                                    0
                                    • jronaldJ jronald

                                      @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                      "Enter" shall not be enabled as long as the input data is invalid.

                                      How to disable "Enter" conditionally?

                                      @SGaist said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                      No need for a message box.

                                      A message box can tell the user what's wrong, it is helpful.

                                      SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                      How to disable "Enter" conditionally?

                                      Tie it to a validator's validity state.

                                      @jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:

                                      A message box can tell the user what's wrong, it is helpful.

                                      A UI that is explicit with regard to what it expects is better.

                                      Do you really like to enter information in an application to just to have a message tell you that you have to do it all over again because of various constraints ?
                                      Constraints that are usually lost as you need to close the dialog to access the UI again ?

                                      Make things simple and clear:

                                      • If you need a number in a range use a QSpinBox
                                      • If you need a line of text with requirements use a QLineEdit with a suitable validator.
                                        Add an icon beside it with a QToolTip giving the expected input or a label underneath it with that advice.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      3

                                      • Login

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