×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    16

    Converting qreal to qstring

    Hey forumnokia,

    Im trying to convert a qreal to a QString. I tried using the QString::setNum which is giving me an error. I added the simple code and error message i got, but can't figure out how to write and use the object.



    Also, if anyone knows another, maybe smarter way to convert a qreal to a QString, im very open for solutions.

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Converting qreal to qstring

    QString::number

    Or,
    Code:
    string1.setNum(real1,'g', 6);

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    16

    Re: Converting qreal to qstring

    Quote Originally Posted by danhicksbyron View Post
    QString::number

    Or,
    Code:
    string1.setNum(real1,'g', 6);
    I can't call setNum from string1 (its a char). I tried creating a new string and use setNum but the error message tells me its an invalid conversion from 'char*' to 'char¨'.

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Converting qreal to qstring

    Have you looked at the spec? Do you know the difference between a regular function and a static function? How do you expect to set a float value into a char?

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Romania->Felnac
    Posts
    5

    Re: Converting qreal to qstring

    Why do you use char array and don't use QString?

    If you really need char array you can use:
    Code:
    // string is a QString
    //and string.setNum(qrealVaribleName); //was used to get the string representation of qreal variable
    
    const char* str = string.toLatin1().data(); //valid until the QString goes out of scope or you modify the QString
    
    // you can copy the c-style string if you really need a copy (strcpy)
    Last edited by Zlatomir; 2010-12-14 at 19:20.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    16

    Re: Converting qreal to qstring

    Quote Originally Posted by Zlatomir View Post
    Why do you use char array and don't use QString?

    If you really need char array you can use:
    Code:
    // string is a QString
    //and string.setNum(qrealVaribleName); //was used to get the string representation of qreal variable
    
    const char* str = string.toLatin1().data(); //valid until the QString goes out of scope or you modify the QString
    
    // you can copy the c-style string if you really need a copy (strcpy)
    Hi there, i don't have to use a char. I just thoght it was the only way to do it.
    Is there an easier way to convert a double to a String?

  7. #7
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Converting qreal to qstring

    Is there an easier way to convert a double to a String?
    RTFM: http://doc.qt.nokia.com/4.7-snapshot....html#number-2

Similar Threads

  1. How to use QString::number
    By nokia-5530 in forum Qt
    Replies: 0
    Last Post: 2010-11-18, 20:25
  2. char to QString?
    By damodharan in forum Qt
    Replies: 4
    Last Post: 2010-07-20, 15:03
  3. exception occurs when converting QString to std::string
    By kimitake in forum [Archived] Qt SDKs and Tools
    Replies: 4
    Last Post: 2010-04-29, 17:08
  4. Rounding float/qreal numbers
    By danhicksbyron in forum Qt
    Replies: 3
    Last Post: 2010-03-13, 03:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×