Results 1 to 4 of 4

Thread: Problem with displaying float value

  1. #1

    Default Problem with displaying float value

    Hi,
    I'm trying to display float value on the screen (temperature, 27,2 °C for example) but I'm getting just integer value (27,0°C).

    Part of temperatue.cpp:

    Qt Code:
    1. void Temperature::setValue(int value)
    2. {
    3. this->value=value;
    4. update();
    5. }
    6.  
    7.  
    8. void Temperature::paint(QPainter* painter)
    9. {
    10. ...
    11. float x = (((((value)-6400)*120)/25600)-40);
    12. digitalValue = QString("%1").arg(x,0,'f',1);
    13. painter->drawText(0,0, 40,20, Qt::AlignRight, digitalValue);
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with displaying float value

    The problem is here
    Qt Code:
    1. float x = (((((value)-6400)*120)/25600)-40);
    To copy to clipboard, switch view to plain text mode 
    All factors are integers, then the result is an integere value converted to float. Try this
    Qt Code:
    1. float x = (((((value)-6400)*120.0)/25600)-40);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. float x = ((((static_cast<float>(value)-6400)*120)/25600)-40);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3

    Default Re: Problem with displaying float value

    Hi,

    I think that "setValue()" should take float instead of int argument.
    Otherwise value which you're passing will always be converted to int.

    Btw. line:
    Qt Code:
    1. float x = (((((value)-6400)*120)/25600)-40);
    To copy to clipboard, switch view to plain text mode 
    for "value"(s) such that:
    Qt Code:
    1. fabs(value-6400) < 25600/120
    To copy to clipboard, switch view to plain text mode 
    will always give you -40. But maybe that's what you want...

  4. #4

    Default Re: Problem with displaying float value

    Ok, I solved it on my own way.

    Now, the code looks:

    Qt Code:
    1. void Temperatura::setValue(float value)
    2. {
    3. this->value=value;
    4. update();
    5. }
    6.  
    7.  
    8. void Temperatura::paint(QPainter* painter)
    9. {
    10. ...
    11. float x = (float)(((value-6400)*120)/25600)-40.0;
    12. digitalValue = QString("%1").arg(x,0,'f',1);
    13. painter->drawText(0,0, 40,20, Qt::AlignRight, digitalValue);
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

    Anyway, thanks mcosta and sebastian for answers.

Similar Threads

  1. Problem in displaying RGB32 data
    By bmn in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2008, 14:30
  2. Problem in displaying the ComboBox list
    By raghvendramisra in forum Qt Tools
    Replies: 4
    Last Post: 28th February 2008, 13:25
  3. Replies: 7
    Last Post: 15th January 2008, 14:40
  4. New to QT, problem displaying main window...
    By McCall in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 14:27
  5. Table Model / View Problem -- Data Not Displaying
    By jhendersen in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2007, 06:45

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
  •  
Qt is a trademark of The Qt Company.