Results 1 to 4 of 4

Thread: label setText value is 0

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default label setText value is 0

    spinbox reads value input by user. then the value is multiplied with float value .and
    displayed as output in a label. but the value is 0(incorrect).

    Code:

    Qt Code:
    1. QDoubleSpinBox *myspinBox;
    2. QLabel *labelText;
    3.  
    4.  
    5. QObject::connect(myspinBox,SIGNAL(valueChanged(QString)),this,SLOT(setnewText(QString)));
    6.  
    7. void myDialog::setnewText(QString newinput)
    8. {
    9. newinputvalue = newinput;
    10. multiplication();
    11.  
    12. }
    13.  
    14. void myDialog::multiplication()
    15. {
    16. float floatnum ;
    17. float floatmultiplywith;
    18. bool ok;
    19.  
    20. floatnum = newinputvalue .toFloat(&ok);
    21.  
    22. QString newValue = QString::number(floatmultiplywith*floatnum );
    23. labelText->setText(newValue );
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: label setText value is 0

    floatmultiplywith is not defined (= 0 as default I guess...), and did you check if your conversion to float works?

  3. #3
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: label setText value is 0

    Quote Originally Posted by Lykurg View Post
    floatmultiplywith is not defined (= 0 as default I guess...), and did you check if your conversion to float works?
    Yes . floatmultiplywith is not defined.

    I removed the line as below and the conversion is correct:

    Qt Code:
    1. labelText->setSuffix(" slices");
    To copy to clipboard, switch view to plain text mode 

    So now after removing the line of code as above the labelText value is correct!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: label setText value is 0

    What did you do to give floatmultiplywith a value?

    You are using a QSpinBox (QDoubleSpinBox) which will give an int (or double) when value() is called regardless of the prefix or suffix settings. By grabbing the text() value you get a string including the prefix/suffix when you really want the value. Calling cleanText() would be closer but you still have to manually convert to int/double. In short, if you want a number don't ask for the value as a string.

Similar Threads

  1. setText translation
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2009, 15:59
  2. QAction and setText
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 4th February 2009, 14:32
  3. setText and html
    By mattia in forum Newbie
    Replies: 11
    Last Post: 31st January 2008, 19:10
  4. setText in QToolButton ( qt4 )
    By joseph in forum Qt Programming
    Replies: 23
    Last Post: 5th July 2007, 10:26
  5. setText not working
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 13:05

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.