Results 1 to 2 of 2

Thread: QString to int

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QString to int

    Hi, I've got a class which has function that will run upon instantiation to set various member variables. One of the values in the initialiser list is a QString. This is pretty stupid coding but I haven't figured out a more eloquent way of doing it as of yet but basically, since I'm hard coding this stuff, I'm relying on the fact that I won't have to make any changes and that I don't make mistakes but I digress...

    Basically my QString will be something along the lines of a1.

    I've already written the code that ?parses? the string for the first letter to do consequent processing and class member variable setting with the use of myString.startsWIth("a"), however I also need to somehow use the second value to set member variables.

    Qt Code:
    1. void MyClass::SetVar(QString sVar)
    2. {
    3. //Left line select key
    4. if(sVar.startsWith("a"))
    5. {
    6. this->m_iInt = LEFT_ALIGNED;
    7. if(sVar[1].isDigit())
    8. {
    9. this->m_iInt2 = sVar[1];
    10. }
    11. else QMessageBox::information(this, "Error", "Error");
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    So the question is... since sVar[2] returns a QCharRef, how do I then set my m_iInt2 value to whatever that string number is?

  2. #2
    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

    I think you should take a look at toInt member function.

Similar Threads

  1. Replies: 3
    Last Post: 27th July 2012, 09:30
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. Replies: 8
    Last Post: 25th November 2010, 11:40
  4. Replies: 4
    Last Post: 1st February 2010, 14:21
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.