Results 1 to 10 of 10

Thread: Cannot convert double to QString

  1. #1
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Cannot convert double to QString

    I am running Qt 4.2.2 on FreeBSD 4.8 with gcc 3.4.6. I am working with dollar amounts in doubles but I cannot properly convert these doubles into QStrings for display in a QLabel. As a test I compiled the following very simple code:

    Qt Code:
    1. #include <QtCore>
    2. int main () {
    3. double doublevar = 42.84;
    4.  
    5. qDebug() << QString::number(doublevar);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Which outputs "-0" to the terminal. What could be causing this? The same code in this simple program runs fine on my Linux and Windows boxes. If I use a QVariant as below:
    Qt Code:
    1. #include <QtCore>
    2. int main () {
    3. double doublevar = 42.84;
    4.  
    5. QVariant var(doublevar);
    6. qDebug() << var.toString();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The output is "-0.00000000005965e+224". Again, what could be wrong?

    Thanks
    mAx

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot convert double to QString

    This indeed is strange. The output you experience could be caused by mixups caused by incompatibility between your machine (CPU and OS) and the Qt library, but I think that in this cause nothing should run at all. What is the result of the following?

    Qt Code:
    1. double d = 42.84;
    2. qDebug() << d;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Cannot convert double to QString

    This
    Qt Code:
    1. #include <QtCore>
    2. int main () {
    3. double d = 42.84;
    4. qDebug() << d;
    5. }
    To copy to clipboard, switch view to plain text mode 
    still gives "-0"

    Thanks
    mAx

  4. #4
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot convert double to QString

    What compiler options do you use, maybe there are some special switches for double floating point (64-bit)?
    Software Engineer



  5. #5
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Cannot convert double to QString

    I have not edited the default settings that Qt defaulted to. Here is what is given during a make:

    g++34 -c -pipe -O2 -Wall -W -pthread -D_THREAD_SAFE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../local/Trolltech/Qt-4.2.2/mkspecs/freebsd-g++34 -I. -I../../../local/Trolltech/Qt-4.2.2/include/QtCore -I../../../local/Trolltech/Qt-4.2.2/include/QtCore -I../../../local/Trolltech/Qt-4.2.2/include/QtGui -I../../../local/Trolltech/Qt-4.2.2/include/QtGui -I../../../local/Trolltech/Qt-4.2.2/include -I. -I. -I. -I../../../local/include -o foo.o foo.cpp
    Thanks
    mAx
    Last edited by jacek; 7th February 2007 at 20:44. Reason: changed [code] to [quote]

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot convert double to QString

    And this one?

    Qt Code:
    1. printf("%lf\n", 42.84);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Cannot convert double to QString

    That gives: 42.840000

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot convert double to QString

    Did you compile Qt yourself or do you use some precompiled package? What exact architecture do you have? x86?

  9. #9
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Cannot convert double to QString

    Yes it is x86 and I did compile Qt myself as I could only find Qt 3 for FreeBSD 4.8 in package format.

    Thanks
    mAx

  10. #10
    Join Date
    Dec 2007
    Posts
    4

    Default Re: Cannot convert double to QString

    Some ARM installations have floating point implementations that are not fully IEEE-compliant. In Qt/Embedded 3.3.x, this causes QString::number(double) and related functions to give incorrect results. A fix for this was introduced in Qt 3.3.2: defining the macro QT_QLOCALE_USES_FCVT will make Qt use system library code instead, i.e:

    ./configure -embedded arm -DQT_QLOCALE_USES_FCVT ...

    i am in the face of this problem,i have added the macro,but it doesn't work.

Similar Threads

  1. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 01:50
  2. Convert wstring to QString.
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 14:18
  3. Replies: 2
    Last Post: 12th June 2006, 17:47
  4. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 09:00
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

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.