Results 1 to 2 of 2

Thread: accessing data in a QFile

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default accessing data in a QFile

    hello everyone,
    i have been ssaving some variable values in a file using QFile and QDataStream.
    the variables are of types Q_UINT8, Q_INT8, Q_UINT32, Q_INT32, float, double and even QString...
    write and read operation work wonderfully so far..

    but now i need to be able to access certain variables down the stream..
    so if i have written the stream in the file as:

    QDataStream ds(&aQFile)
    ds<<aUINT8<<aUINT32<<aINT8<<aFloat<<aDouble<<aQStr ing<<anotherINT8..... etcetc

    and now i want to access directly aINT8, how do i do it?
    i read in te documentation at the QFile::at(Offset off) function... i guess the offset is just an integer number of the counted bytes till the beginning of the variable i want to read..
    however i wonder how im gonna compute the offsets..
    mean in the above case its easy to find the offset of aINT8.. it is 5 (bytes) ... or not?
    however how can i get the offset of anotherINT8 ?
    thank you for your help
    nass

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: accessing data in a QFile

    how can i get the offset of anotherINT8 ?
    You have to know your offeset when accesing it, not way around it.
    (so in the example you gave you have to know how many baytes are written before 'anotherINT8')
    But usually, you would use structures in order to serialize data, and not write just like that in to a file.
    So for exmple, you could create a struct or a class called DataBlock, in which each field would have a name and type.
    Like so:
    typedef struct DataBlock {
    Q_UINT32 blockNum;
    Q_UINT8 feild1;
    QString feild2;
    float feild3;
    }
    You then can overload the global QDataStream & operator<<() and QDataStream & operator>>() to make reading and writing data blocks easy.
    All you need then is to know in which block the feild you are looking for is at, read that much blocks, and expose the field.

Similar Threads

  1. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 11:31
  2. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  3. QFtp->List() does not recieve any data
    By nopalot in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2006, 20:42
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.