How to bind QString value in perticular range ?
-
i want to do operation on string which has value between "000" to "999".
NOW when unknown string come at that time i need to replace it with "000".
Is anybody know how to implement this logic ?
my code is written below. there is instead "c" and "". any string can come.
QString sCurrentFuelGaugeFirmwareValue = receive();
if(sCurrentFuelGaugeFirmwareValue == "v" || sCurrentFuelGaugeFirmwareValue == "")
sCurrentFuelGaugeFirmwareValue = "000";
if(sNewFuelGaugeFirmwareValue == "")
sNewFuelGaugeFirmwareValue = "000"; -
@J-Hilk said in How to bind QString value in perticular range ?:
I'm disgusted, that this actually works, feels like Python/JavaScript
It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.
It doesn't fully work.
"1111" >= "000"and"1111" <= "999"are both true.Again I'm not with you. That is a 4-digit string. Just like my
0001example above. I stated that would be treated as acceptable, where the OP might want unacceptable. I said my suggestion is "lazy" because of this, it works assuming the input is 3 characters long.Equally @TheGringerEye's solution treats, say, a 2-digit
12as valid, we don't know what the OP wants done with that. These solutions depend on precisely what the OP wants treated how....If the OP wants the "lazy" way it could be improved with
if (! (str.length() == 3 && str >= "000" && str <= "999")) str = "000";EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....
-
i want to do operation on string which has value between "000" to "999".
NOW when unknown string come at that time i need to replace it with "000".
Is anybody know how to implement this logic ?
my code is written below. there is instead "c" and "". any string can come.
QString sCurrentFuelGaugeFirmwareValue = receive();
if(sCurrentFuelGaugeFirmwareValue == "v" || sCurrentFuelGaugeFirmwareValue == "")
sCurrentFuelGaugeFirmwareValue = "000";
if(sNewFuelGaugeFirmwareValue == "")
sNewFuelGaugeFirmwareValue = "000";@Qt-embedded-developer said in How to bind QString value in perticular range ?:
i want to do operation on string which has value between "000" to "999".
NOW when unknown string come at that time i need to replace it with "000".A "lazy" way would be
if (! (str >= "000" && str <= "999")) str = "000";EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....
A more robust way would be to use QRegularExpression.
-
@Qt-embedded-developer said in How to bind QString value in perticular range ?:
i want to do operation on string which has value between "000" to "999".
NOW when unknown string come at that time i need to replace it with "000".A "lazy" way would be
if (! (str >= "000" && str <= "999")) str = "000";EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....
A more robust way would be to use QRegularExpression.
@JonB said in How to bind QString value in perticular range ?:
if (! (str >= "000" && str <= "999"))
str = "000";I'm disgusted, that this actually works, feels like Python/JavaScript
I wonder how the >= is actually implemented 🤔
-
@JonB said in How to bind QString value in perticular range ?:
if (! (str >= "000" && str <= "999"))
str = "000";I'm disgusted, that this actually works, feels like Python/JavaScript
I wonder how the >= is actually implemented 🤔
@J-Hilk
Well I never test anything, I just type in my proposed answers.I'm lost, why disgusted? What do you mean about "how
>=is implemented"? It's a straight string comparison, and digits are guaranteed to be consecutive characters or code points or whatever they call them. It's not going to convert to the number, if that's what you mean.Just to be clear, if the input were, say,
0001this test would treat that as valid, and not alter to000, which a regular expression could correctly catch. But if the OP knows the input is always 3 characters it suffices. Of course I would use the reg ex, but the OP might not fancy that.... -
QString test("63"); QIntValidator valid(0, 999); int pos; if (QValidator::State::Acceptable != valid.validate(test, pos)) { test = QLatin1Char('0'); } test = test.sprintf("%03i", test.toInt()); -
@JonB said in How to bind QString value in perticular range ?:
if (! (str >= "000" && str <= "999"))
str = "000";I'm disgusted, that this actually works, feels like Python/JavaScript
I wonder how the >= is actually implemented 🤔
@J-Hilk said in How to bind QString value in perticular range ?:
I'm disgusted, that this actually works, feels like Python/JavaScript
It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.
-
QString test("63"); QIntValidator valid(0, 999); int pos; if (QValidator::State::Acceptable != valid.validate(test, pos)) { test = QLatin1Char('0'); } test = test.sprintf("%03i", test.toInt());@TheGringerEye it gives below error
error: 'QValidator::State' is not a class or namespace
if (QValidator::State::Acceptable != valid.validate(test, pos))
^ -
@TheGringerEye it gives below error
error: 'QValidator::State' is not a class or namespace
if (QValidator::State::Acceptable != valid.validate(test, pos))
^@Qt-embedded-developer
#include <QIntValidator> -
@J-Hilk
Well I never test anything, I just type in my proposed answers.I'm lost, why disgusted? What do you mean about "how
>=is implemented"? It's a straight string comparison, and digits are guaranteed to be consecutive characters or code points or whatever they call them. It's not going to convert to the number, if that's what you mean.Just to be clear, if the input were, say,
0001this test would treat that as valid, and not alter to000, which a regular expression could correctly catch. But if the OP knows the input is always 3 characters it suffices. Of course I would use the reg ex, but the OP might not fancy that.... -
@J-Hilk said in How to bind QString value in perticular range ?:
I'm disgusted, that this actually works, feels like Python/JavaScript
It doesn't fully work. "1111" >= "000" and "1111" <= "999" are both true.
It doesn't fully work.
"1111" >= "000"and"1111" <= "999"are both true.Again I'm not with you. That is a 4-digit string. Just like my
0001example above. I stated that would be treated as acceptable, where the OP might want unacceptable. I said my suggestion is "lazy" because of this, it works assuming the input is 3 characters long.Equally @TheGringerEye's solution treats, say, a 2-digit
12as valid, we don't know what the OP wants done with that. These solutions depend on precisely what the OP wants treated how....If the OP wants the "lazy" way it could be improved with
if (! (str.length() == 3 && str >= "000" && str <= "999")) str = "000";EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....
-
It doesn't fully work.
"1111" >= "000"and"1111" <= "999"are both true.Again I'm not with you. That is a 4-digit string. Just like my
0001example above. I stated that would be treated as acceptable, where the OP might want unacceptable. I said my suggestion is "lazy" because of this, it works assuming the input is 3 characters long.Equally @TheGringerEye's solution treats, say, a 2-digit
12as valid, we don't know what the OP wants done with that. These solutions depend on precisely what the OP wants treated how....If the OP wants the "lazy" way it could be improved with
if (! (str.length() == 3 && str >= "000" && str <= "999")) str = "000";EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF IN LATER POST....
@JonB Yes, if you need not to skip "0001", then you need to add a check for the length of the string
-
EDIT MY BAD, NOT ENOUGH COFFEE YET, I CORRECT MYSELF, SORRY....
Sorry, not thinking clearly at all.
If input can contain non-digits, my "lazy":
str >= "000" && str <= "999"would not be right at all,
0AZwould be acceptable!So either convert to a number and compare numerically (e.g.
QIntValidatoror other) or do a regular expression, depending on what is wanted/not wanted.... -
@Qt-embedded-developer
#include <QIntValidator>@TheGringerEye said in How to bind QString value in perticular range ?:
#include <QIntValidator>
after adding header file still i get error QValidator::State is not a class or name space
-
@TheGringerEye said in How to bind QString value in perticular range ?:
#include <QIntValidator>
after adding header file still i get error QValidator::State is not a class or name space
@Qt-embedded-developer It should be
if (QValidator::Acceptable != valid.validate(test, pos))Easy to find out actually by reading documentation and considering how C++ enums work...