Converting QString to int

Hi everyone,

I'm trying to convert a QString into an int. I have read the documentation (https://doc.qt.io/qt-5/qstring.html#toInt) but I don't understand the purpose of the *ok pointer in the toInt() function. What I'm trying to do is something as simple as converting QString Number = "1234" into an integer. That's all.

Any help would be greatly appreciated. Thank you.
Directly from the example.
1
2
3
4
QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
int dec = str.toInt(&ok, 10);       // dec == 0, ok == false 

Quite simply, the ok flag tells you whether the conversion was successful or not.

I mean, you don't want to stumble through
1
2
3
4
5
QString str = "I'm a banana";
int launchCode = str.toInt();
if ( launchCode == magicCode ) {
    // rain death and destruction upon thyne enemies.
}
Thank you salem c. Now I understand I could simply do something like this:

1
2
3
4
5
6
7
So basically I could do something as :
QString str = "1234";
bool ok;
int num = str.toInt(&ok);
if (!ok) {
  cout << "Conversion failed. Repeat conversion" <<endl;
}


ok pointers is just to check errors but should not affect my conversion from QString to int.
Topic archived. No new replies allowed.