Results 1 to 4 of 4

Thread: Strings from LineEdit to Integers

  1. #1
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Strings from LineEdit to Integers

    Hi,

    i want to calculate numbers obtained from a QLineEdit.
    So i've used a QTextStream to pass the string to an integer like this:
    QTextStream ts(&str);
    ts << lineEditValue->text();
    ts >> strInteger;
    lineEditResult->setText(strInteger);
    So for now i'm just trying to get the string from lineEditValue into lineEditResult via a QTextStream (then i want to pass an integer from lineEditValue into lineEditResult).

    Clue anyone?
    Thx,
    Misko

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strings from LineEdit to Integers

    You have QString::toUInt()to retrieve the integer value. No need for a QTextStream.

    Regards

  3. #3
    Join Date
    Jul 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strings from LineEdit to Integers

    Hi Marcel,
    Thx again!
    Here's why I asked for it:
    QString strA = lineEditA->text();
    QString strB = lineEditB->text();
    bool ok;
    float lengthA = strA.toFloat(&ok);
    float lengthB = strB.toFloat(&ok);
    c = sqrt(lengthA*lengthA + lengthB*lengthB);
    QString strHypothenusa = QString("%1").arg(c);
    lineEditHypothenusa->setText(strHypothenusa);

    As you can figure, it asks for 2 floats taken from LineEdits, calculates the hypothenusa and puts the value into another LineEdit.
    Of course, no error-checking has been done (like entering negative numbers, ...). But for now is sufficient!

    Regards,
    Misko

  4. #4
    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: Strings from LineEdit to Integers

    Quote Originally Posted by Misko View Post
    As you can figure, it asks for 2 floats taken from LineEdits, calculates the hypothenusa and puts the value into another LineEdit.
    Of course, no error-checking has been done (like entering negative numbers, ...). But for now is sufficient!
    QDoubleValidator, QDoubleSpinBox
    J-P Nurmi

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.