Results 1 to 9 of 9

Thread: QString to int

  1. #1
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Platforms
    Unix/X11 Windows

    Default QString to int

    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:

    Qt Code:
    1. ui->lineEdit->setInputMask("000.000.000-00");
    2.  
    3. QString cpf = ui->lineEdit->text();
    4. cpf.replace(".","");
    5. cpf.replace("-","");
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to int

    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()

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString to int

    You just need to validate, or do you need the numbers for further calculations?
    If you want just validation you can use QRegExp

    And if you want integers you can use the split member function, witch returns a QStringList, and then iterate the list and use toInt

  4. #4
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Platforms
    Unix/X11 Windows

    Default Re: QString to int

    Quote Originally Posted by Zlatomir View Post
    You just need to validate, or do you need the numbers for further calculations?
    If you want just validation you can use QRegExp

    And if you want integers you can use the split member function, witch returns a QStringList, and then iterate the list and use 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!
    Last edited by itallor; 1st December 2010 at 22:34.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString to int

    What part you couldn't do?

    If you have the QString, you can do the following:
    Qt Code:
    1. QString str = "11.22.33";
    2.  
    3. QStringList list1 = str.split(".");
    4. //the list1 will be a list that contains three strings "11", "22" and "33"
    To copy to clipboard, switch view to plain text mode 
    Then you need to iterate over the QStringList and use toInt() on the strings that it contains:
    Qt Code:
    1. //maybe you will need some data structure (for example a QVector) that will contain the integers
    2. QVector<int> vec;
    3.  
    4. QStringList::Iterator iterator;
    5. for (iterator = list1.begin(); iterator != list1.end(); ++iterator)
    6. //add the int to the QVector
    7. vec.push_back(iterator->toInt());
    8. //Then you have all the values in the QVector vec
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString to int

    This thread is just taking too long.

    Qt Code:
    1. ui->lineEdit->setInputMask("000.000.000-00");
    2. //...
    3. QString cpf = ui->lineEdit->text();
    4. QRegExp rx("(\\d{3})\\.(\\d{3})\\.(\\d{3})-(\\d{2})");
    5. if(!rx.exactMatch(cpf)){
    6. // something wrong
    7. }
    8. QVector<int> vals;
    9. for(int i = 1; i<=rx.captureCount();i++){
    10. vals << rx.cap(i).toInt();
    11. }
    12. return someCheck(vals); // check the values here
    To copy to clipboard, switch view to plain text mode 

    Wrap this all into a validator api and set it on the line edit.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    itallor (3rd December 2010)

  8. #7
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Platforms
    Unix/X11 Windows

    Default Re: QString to int

    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:

    Qt Code:
    1. QRegExp rx("(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})(\\d{1})");
    2.  
    3. if(!rx.exactMatch(cpf))
    4. ui->okButton->setEnabled(false);
    5.  
    6. QVector<int> cpfV;
    7.  
    8. for(int i=1;i<=rx.captureCount();i++)
    9. cpfV << rx.cap(i).toInt();
    To copy to clipboard, switch view to plain text mode 

    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?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString to int

    You can definitely skip the "{1}" parts. But in general you can substitute the second part of my code with:
    Qt Code:
    1. QStringList strList = rx.capturedTexts();
    2. strList.removeFirst();
    3. qlonglong val = strList.join('').toLongLong();
    To copy to clipboard, switch view to plain text mode 

    Hmm... or maybe I misunderstood what you wanted... I don't really see any sense of all this.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString to int

    what i needed was that all my numbers became one position at my Vector<int>
    What do you mean?
    Last edited by MattPhillips; 3rd December 2010 at 01:17.

Similar Threads

  1. Replies: 8
    Last Post: 25th November 2010, 11:40
  2. With QString create a QString&
    By avis_phoenix in forum Newbie
    Replies: 1
    Last Post: 21st April 2010, 22:05
  3. Replies: 4
    Last Post: 1st February 2010, 14:21
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.