Results 1 to 8 of 8

Thread: probleme with cast from double to QString

  1. #1
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default probleme with cast from double to QString

    hi
    i have a C++ class that evaluate an arithmitic expression like 9+8-(9/5)
    in the consol i have a good result.
    but if i instantiate my class in a Qt project and pute the result in QLineEdit i have 0

    a slot that instantate and calclate the expression :
    Qt Code:
    1. void calculatrice::evaluateClicked()
    2. {
    3. arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
    4. QString st;
    5. st.setNum( e->evluate() );
    6. outputeLE->setText( st );
    7. }
    To copy to clipboard, switch view to plain text mode 
    evluate() return a double

    thanks
    ++

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: probleme with cast from double to QString

    What does ur arithmeticExpression class take in ctor arguments ??

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: probleme with cast from double to QString

    Assuming your class does return the correct value (have you checked to make sure?) setting the text of QLineEdit this way ought to work, although I would probably use QString's static number() function to make it cleaner, like this:

    arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
    outputeLE->setText(QString::number(e->evaluate()));

    The only real problem I see here is a memory leak.

  4. #4
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: probleme with cast from double to QString

    Hi,

    I have also checked a simple example:
    Qt Code:
    1. double value = 9+8-(9/5);
    2. st.setNum(value);
    3. qDebug()<<st;
    To copy to clipboard, switch view to plain text mode 
    Output: "16".
    I would suggest checking the "evaluate" method. Maybe it do not remove/recognize one of the operational signs, etc.
    Maybe you could paste the "evaluate" method body?
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: probleme with cast from double to QString

    Quote Originally Posted by maverick_pol View Post
    I have also checked a simple example:
    Qt Code:
    1. double value = 9+8-(9/5);
    2. st.setNum(value);
    3. qDebug()<<st;
    To copy to clipboard, switch view to plain text mode 
    Output: "16".
    That's because you calculate with integers. Storing the result to a double doesn't fix the rounding errors. You should rewrite the division part as "(9/5.0)", "(9.0/5)" or "(9.0/5.0)".
    J-P Nurmi

  6. #6
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: probleme with cast from double to QString

    Quote Originally Posted by aamer4yu View Post
    What does ur arithmeticExpression class take in ctor arguments ??
    it take std::string

    Quote Originally Posted by JimDaniel View Post
    Assuming your class does return the correct value (have you checked to make sure?) setting the text of QLineEdit this way ought to work, although I would probably use QString's static number() function to make it cleaner
    i have the same probleme on using QString::number()

    Qt Code:
    1. void calculatrice::evaluateClicked()
    2. {
    3. arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
    4. outputeLE->setText( QString::number(e->evluate()) );
    5. }
    To copy to clipboard, switch view to plain text mode 


    i have the good resulte printed in kdevelop output when i click on evaluate button



    thaks
    ++

  7. #7
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: probleme with cast from double to QString

    Have you stepped this through in a debugger or by other means to see exactly where the algorithm goes wrong? Seems like you could easily narrow it down a bit.

    Apparently the slot is being called, as e->evaluate() is being called, and apparently it calculates the correct value as seen in your output window. But are you completely sure it returns the correct value to the function? Add an intermediary double variable to the function to hold the return value, then see what that is:

    double d = e->evaluate();
    //check what 'd' is...

    I'm with the others who think it must be a bug in evaluate().

    And if you have to create an object on the heap in this case, add this to the end of your function:

    delete e;
    e = 0;

  8. The following user says thank you to JimDaniel for this useful post:

    hbill (16th May 2008)

  9. #8
    Join Date
    Mar 2008
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: probleme with cast from double to QString

    Quote Originally Posted by JimDaniel View Post
    Have you stepped this through in a debugger or by other means to see exactly where the algorithm goes wrong? Seems like you could easily narrow it down a bit.

    Apparently the slot is being called, as e->evaluate() is being called, and apparently it calculates the correct value as seen in your output window. But are you completely sure it returns the correct value to the function? Add an intermediary double variable to the function to hold the return value, then see what that is:

    double d = e->evaluate();
    //check what 'd' is...

    I'm with the others who think it must be a bug in evaluate().

    And if you have to create an object on the heap in this case, add this to the end of your function:

    delete e;
    e = 0;
    than kyou for all

    in my fonction
    Qt Code:
    1. double arithmeticExpression::evluate()
    To copy to clipboard, switch view to plain text mode 
    i make
    Qt Code:
    1. double d = pileDouble.getTopData() ;
    2. return d;
    To copy to clipboard, switch view to plain text mode 
    it work now
    it was
    Qt Code:
    1. return pileDouble.getTopData();
    To copy to clipboard, switch view to plain text mode 
    thank you
    ++

Similar Threads

  1. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.