I am taking an input from line edit box which is QString by default. I further want to convert this value from QString to quint32. Any suggestions on how to implement this.
I am taking an input from line edit box which is QString by default. I further want to convert this value from QString to quint32. Any suggestions on how to implement this.
Hi
If it's a QString then you can use the built in function QString::toUInt(). Read the Qt Assistant docs.
I want to convert from QString to quint32 & not unsigned int. I have checked Qt Assistant docs, but was not able to find anything there.
My code is below, but its not working. Can you suggest corrections in it. Thanx in advance.
QString phrase, bobkey, bobid;
phrase = stringlineEdit->text();
bobkey = keylineEdit->text();
bob_initialkey = (quint32)bobkey.toInt();
bob_userid = (quint32)useridlineEdit->text().toInt();
resulttextEdit->append( "BOB Initial Key: " + display_uint(bob_initialkey) ); //Here output is 0.
resulttextEdit->append( "BOB user ID: " + display_uint(bob_userid) ); //Here output is 0.
qDebug() << "BOB Initial Key: " << bobkey; //This is showing correct value
qDebug() << "BOB Initial Key: " << bob_initialkey; //Here output is 0.
Do like this:
I guess that you don't take the values from that lineEdit, and the string remains empty.Qt Code:
QString testInt = "4294967295"; /*make sure that in QString you don't have a big number, that will not fit in unsigned int32*/ quint32 test = testInt.toUInt();To copy to clipboard, switch view to plain text mode
You can use signal and slots to connect the text changed in QLineEdit to a slot (defined by you) witch will initialize the string variables.
If you can't get it to work post the OS, Qt version... and a sample compilable example that replicate the issue.
its working fine when i am giving input as decimal value. but when i enter hexadecimal value, it displays value as 0.
Qt Code:
quint32 test = testInt.toUInt(); str = "FF"; bool ok; quint32 hex = str.toUInt(&ok, 16); // hex quint32 dec = str.toUInt(&ok, 10); // dec qDebug() << test; qDebug() << hex; qDebug() << dec;To copy to clipboard, switch view to plain text mode
sksingh73 (2nd July 2010)
Thanks buddy.
Just a little note:
Or you can use string that begins with "0x" and base 16 will be used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.Qt Code:
bool ok; // you can check the bool variable to see if the conversion worked quint32 hex = str.toUInt(&ok, 16); // hex //here the ok will be true, and quint32 hex will be 255 quint32 dec = str.toUInt(&ok, 10); // dec //here the bool variable will become false because the FF isn't an integer, and quint32 dec will be 0;To copy to clipboard, switch view to plain text mode
Bookmarks