Results 1 to 5 of 5

Thread: Conversion of Qstring to Qint64

  1. #1
    Join Date
    Nov 2013
    Posts
    15
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Conversion of Qstring to Qint64

    I have a LineEdit and want to convert the value to qint64 (long) for some calculation. But I always get a problem and the value return 0. How can I solve it?

    Qt Code:
    1. inline long GetInteger64FromStatic(QLineEdit* lineEdit)
    2. {
    3. QString text;
    4. qint64 nValue = 0;
    5. //bool convertOK;
    6. bool ok;
    7.  
    8. nValue = lineEdit->text().toLong(&ok,10);
    9.  
    10. return nValue;
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Conversion of Qstring to Qint64

    If you want a qint64 you should call QString::toLongLong().
    long has the problem that on Windows, or at least the Microsoft C++ compiler, a long is only 4 bytes, basically equivalent of an int.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2013
    Posts
    15
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Conversion of Qstring to Qint64

    when I try this code I still get 0 for value = 20.5. But I need value=20. How can I solve it?

    Qt Code:
    1. inline qint64 GetInteger64FromStatic(QLineEdit* lineEdit)
    2. {
    3. QString text;
    4. qint64 nValue = 0;
    5. //bool convertOK;
    6. bool ok;
    7.  
    8. nValue = lineEdit->text().toLongLong(&ok,10);
    9.  
    10. return nValue;
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by citix; 21st January 2014 at 07:43.

  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: Conversion of Qstring to Qint64

    Well, yes, 20.5 is not an integer so you get zero and the ok flag set to false. If you need to accept floating point numbers then you need to convert to a double and then apply whatever rounding is appropriate.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Conversion of Qstring to Qint64

    20.5 is not an integer value, it contains a .

    Instead this is a floating point value, so have a look at QString::toDouble()

    Cheers,
    _

Similar Threads

  1. Enum To QString Conversion
    By yagabey in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2012, 14:28
  2. BSTR to QString Conversion
    By bismitapadhy in forum Qt Programming
    Replies: 9
    Last Post: 16th February 2012, 12:51
  3. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  4. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17
  5. qint64 to QString
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th March 2006, 08:21

Tags for this Thread

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.