How to popup a message box when QAbstractModel::setData got invalid data?
-
@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.
@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.
-
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?
-
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?
@jronald
Neither of those is correct behaviour. https://doc.qt.io/qt-5/qabstractitemmodel.html#setData is perfectly clear:Returns
true
if successful; otherwise returnsfalse
.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()
. -
@jronald
Neither of those is correct behaviour. https://doc.qt.io/qt-5/qabstractitemmodel.html#setData is perfectly clear:Returns
true
if successful; otherwise returnsfalse
.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()
.@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".
-
@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".
@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
returnsfalse
. -
@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".
@jronald
I have already told you whatsetData()
must do. If you want to act on afalse
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 :)
-
@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
returnsfalse
.@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. -
@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. -
@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?
@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).
-
@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).
Anyway to implement the procedure as below:
- User input invalid data and press "Enter"
- Message box popups to tells the user what's wrong
- 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
-
@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?
@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 callingsetData()
) and display the message there and then? -
@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 callingsetData()
) and display the message there and then?@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. -
@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.@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 fromsetData()
and doing theemit
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.
-
Anyway to implement the procedure as below:
- User input invalid data and press "Enter"
- Message box popups to tells the user what's wrong
- 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
Hi,
@jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:
- 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.
-
Hi,
@jronald said in How to popup a message box when QAbstractModel::setData got invalid data?:
- 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.
@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.
-
@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.
@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.