PDA

View Full Version : QString to int



Atomic_Sheep
7th September 2012, 09:31
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.



void MyClass::SetVar(QString sVar)
{
//Left line select key
if(sVar.startsWith("a"))
{
this->m_iInt = LEFT_ALIGNED;
if(sVar[1].isDigit())
{
this->m_iInt2 = sVar[1];
}
else QMessageBox::information(this, "Error", "Error");
}
}

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?

Zlatomir
7th September 2012, 09:52
I think you should take a look at toInt (http://qt-project.org/doc/qt-4.8/qstring.html#toInt) member function.