PDA

View Full Version : QString to int



itallor
1st December 2010, 13:38
Hello everyone, i'm trying to develop an program that validate a string that will have just numbers, but to do this i need to make the string become an int number array, i'm using a mask that just allows the user to insert numbers, and i replaced all special characters like "." , "-" that i used in the mask and my string now is just numbers.

Like the code:



ui->lineEdit->setInputMask("000.000.000-00");

QString cpf = ui->lineEdit->text();
cpf.replace(".","");
cpf.replace("-","");


Please help me make my string become an int array, because i need to work with each number at a time to compare them and validate the whole number!

Thank you very much!

FelixB
1st December 2010, 13:58
why don't you use several LineEdits for the input? Then you can install an QIntValidator on each LineEdit and convert the input with QString::number()

Zlatomir
1st December 2010, 14:42
You just need to validate, or do you need the numbers for further calculations?
If you want just validation you can use QRegExp (http://doc.trollteck.com/4.7/qregexp.html#details)

And if you want integers you can use the split (http://doc.troll.no/4.7/qstring.html#split) member function, witch returns a QStringList (http://doc.troll.no/4.7/qstringlist.html), and then iterate the list and use toInt (http://doc.troll.no/4.7/qstring.html#toInt)

itallor
1st December 2010, 22:21
You just need to validate, or do you need the numbers for further calculations?
If you want just validation you can use QRegExp (http://doc.trollteck.com/4.7/qregexp.html#details)

And if you want integers you can use the split (http://doc.troll.no/4.7/qstring.html#split) member function, witch returns a QStringList (http://doc.troll.no/4.7/qstringlist.html), and then iterate the list and use toInt (http://doc.troll.no/4.7/qstring.html#toInt)

I'll need the numbers for further calculation, the validation will be made under those calculations.

Thank you very much i'll try it.

Edit: I tried to do what you said but i couldn't can you please help me! I need each number to an calculation! Thanks again!

Zlatomir
2nd December 2010, 08:28
What part you couldn't do?

If you have the QString, you can do the following:


QString str = "11.22.33";

QStringList list1 = str.split(".");
//the list1 will be a list that contains three strings "11", "22" and "33"

Then you need to iterate over the QStringList and use toInt() on the strings that it contains:


//maybe you will need some data structure (for example a QVector) that will contain the integers
QVector<int> vec;

QStringList::Iterator iterator;
for (iterator = list1.begin(); iterator != list1.end(); ++iterator)
//add the int to the QVector
vec.push_back(iterator->toInt());
//Then you have all the values in the QVector vec

wysota
2nd December 2010, 10:05
This thread is just taking too long.


ui->lineEdit->setInputMask("000.000.000-00");
//...
QString cpf = ui->lineEdit->text();
QRegExp rx("(\\d{3})\\.(\\d{3})\\.(\\d{3})-(\\d{2})");
if(!rx.exactMatch(cpf)){
// something wrong
}
QVector<int> vals;
for(int i = 1; i<=rx.captureCount();i++){
vals << rx.cap(i).toInt();
}
return someCheck(vals); // check the values here

Wrap this all into a validator api and set it on the line edit.

itallor
2nd December 2010, 22:04
Thank you everyone, to lead me to the right way!

I tried what wysota said but it didn't work like i expected, because what i needed was that all my numbers became one position at my Vector<int> so i did my QRegExp like this:



QRegExp rx("(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\ \d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})");

if(!rx.exactMatch(cpf))
ui->okButton->setEnabled(false);

QVector<int> cpfV;

for(int i=1;i<=rx.captureCount();i++)
cpfV << rx.cap(i).toInt();


And the QRegExp that you gave me was making a vector with 4 elements, but it helped me a lot, thank you so much! And i would like to know if is there anyway of making this QRegExp smaller?

wysota
2nd December 2010, 22:15
You can definitely skip the "{1}" parts. But in general you can substitute the second part of my code with:

QStringList strList = rx.capturedTexts();
strList.removeFirst();
qlonglong val = strList.join('').toLongLong();

Hmm... or maybe I misunderstood what you wanted... I don't really see any sense of all this.

MattPhillips
3rd December 2010, 00:44
what i needed was that all my numbers became one position at my Vector<int>

What do you mean?