Results 1 to 10 of 10

Thread: Conversion from QString to quint32

  1. #1
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Conversion from QString to quint32

    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.

  2. #2
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Conversion from QString to quint32

    Hi
    If it's a QString then you can use the built in function QString::toUInt(). Read the Qt Assistant docs.

  3. #3
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from QString to quint32

    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.

  4. #4
    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: Conversion from QString to quint32


  5. #5
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from QString to quint32

    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.

  6. #6
    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: Conversion from QString to quint32

    Do like this:
    Qt Code:
    1. QString testInt = "4294967295"; /*make sure that in QString you don't have a big number, that will not fit in unsigned int32*/
    2. quint32 test = testInt.toUInt();
    To copy to clipboard, switch view to plain text mode 
    I guess that you don't take the values from that lineEdit, and the string remains empty.
    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.

  7. #7
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from QString to quint32

    its working fine when i am giving input as decimal value. but when i enter hexadecimal value, it displays value as 0.

  8. #8
    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: Conversion from QString to quint32

    Qt Code:
    1. QString testInt = "4294967295"; //biggest value
    2. quint32 test = testInt.toUInt();
    3.  
    4. str = "FF";
    5. bool ok;
    6.  
    7. quint32 hex = str.toUInt(&ok, 16); // hex
    8. quint32 dec = str.toUInt(&ok, 10); // dec
    9.  
    10. qDebug() << test;
    11. qDebug() << hex;
    12. qDebug() << dec;
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Zlatomir for this useful post:

    sksingh73 (2nd July 2010)

  10. #9
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conversion from QString to quint32

    Thanks buddy.

  11. #10
    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: Conversion from QString to quint32

    Just a little note:
    Qt Code:
    1. bool ok; // you can check the bool variable to see if the conversion worked
    2.  
    3. quint32 hex = str.toUInt(&ok, 16); // hex
    4. //here the ok will be true, and quint32 hex will be 255
    5. quint32 dec = str.toUInt(&ok, 10); // dec
    6. //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 
    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.

Similar Threads

  1. BSTR to QString Conversion
    By bismitapadhy in forum Qt Programming
    Replies: 9
    Last Post: 16th February 2012, 12:51
  2. Qstring conversion UTF8
    By harleyskater in forum Qt Programming
    Replies: 12
    Last Post: 25th June 2010, 11:59
  3. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 22:16
  4. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  5. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.