Results 1 to 5 of 5

Thread: QTextStream, doubles and ints

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextStream, doubles and ints

    Hi every one.

    I would appreciate it if some one can point me in the right direction. The problem i am having is, I have a line that i read from a file with doubles and ints in the line example

    1 2 3 4.5 6.7 2.3
    I want to put these numbers in ints and doubles. What i tried to do was.

    Qt Code:
    1. int one, two, three;
    2. double oned, twod threed;
    3. QTextStream ssFile, line;
    4. ssFile.setDevice(&file);
    5. QString stringFile = ssFile.readLine();
    6. line.setString(&stringFile);
    7. line >> one >> two >> three >> oned >> twod >>threed;
    To copy to clipboard, switch view to plain text mode 
    Needles to say it did not work. is there an easy way in doing this.
    Thanks in advance

    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, doubles and ints

    Hi,

    Here you have "1 2 3 4.5 6.7 2.3" on a QSting, right?
    Qt Code:
    1. QString stringFile = ssFile.readLine();
    To copy to clipboard, switch view to plain text mode 
    So then, you can use "space" as character separator using "QString::split"
    Then you can do a loop to all QStringList elements converting to the right variable. Also, you can check if the QString element of the QStringList contains "." and if it contains it you can assign it to a double variable else to a int variable.
    Last edited by ^NyAw^; 18th March 2009 at 10:33.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream, doubles and ints

    Thank's I thought of doing it that way.
    I just want to know if there are a way like int std c++ one can use std::stringstream and read it in the way I tried to with Qt.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream, doubles and ints

    Hi,

    Using QTextStream you can't because all is text, so then you have to parse the data.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream, doubles and ints

    If one look at the example in the manual. I thought it would work that way.
    By default, when reading numbers from a stream of text, QTextStream will automatically detect the number's base representation. For example, if the number starts with "0x", it is assumed to be in hexadecimal form. If it starts with the digits 1-9, it is assumed to be in decimal form, and so on. You can set the integer base, thereby disabling the automatic detection, by calling setIntegerBase(). Example:
    QTextStream in("0x50 0x20");
    int firstNumber, secondNumber;

    in >> firstNumber; // firstNumber == 80
    in >> dec >> secondNumber; // secondNumber == 0

    char ch;
    in >> ch; // ch == 'x'

Similar Threads

  1. QTreeView questions about view doubles and more
    By davisjamesf in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00:39

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.