Results 1 to 2 of 2

Thread: Problem using multiple QDataStreams

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem using multiple QDataStreams

    I have a large array of floats and need to write sections of the array out to multiple files. The following code demonstrates an approach (that didn't work):

    Qt Code:
    1. const int FILES = 8;
    2. QVector<QDataStream> v;
    3.  
    4. for (int i=0; i<FILES; i++)
    5. {
    6. QString qstr("f");
    7. qstr.append(QString::number(i));
    8. qstr.append(".dat");
    9. QFile f(qstr);
    10. f.open(QIODevice::WriteOnly | QIODevice::Append);
    11. QDataStream ds(&f);
    12. v.append(ds);
    13. }
    14.  
    15. for (int cnt=0; cnt<99; cnt++)
    16. {
    17. for (int i=0; i<FILES; i++)
    18. {
    19. QDataStream ds = v.at(i);
    20. ds << 999.9; // test
    21. }
    22. }
    23. for (int i=0; i<FILES; i++)
    24. {
    25. v.at(i).device().close();
    26. }
    To copy to clipboard, switch view to plain text mode 

    This code gives the following compiler error:

    QDataStream::QDataStream(const QDataStream&) is private

    So, my solution was to replace the QVector<QDataStream> with pointers to QDataStream:


    Qt Code:
    1. const int FILES = 8;
    2. QVector<QDataStream *> v;
    3.  
    4. for (int i=0; i<FILES; i++)
    5. {
    6. QString qstr("f");
    7. qstr.append(QString::number(i));
    8. qstr.append(".dat");
    9. QFile f(qstr);
    10. f.open(QIODevice::WriteOnly | QIODevice::Append);
    11. QDataStream *ds = new QDataStream(&f);
    12. v.append(ds);
    13. }
    14.  
    15. for (int cnt=0; cnt<99; cnt++)
    16. {
    17. for (int i=0; i<FILES; i++)
    18. {
    19. QDataStream *ds = v.at(i);
    20. *ds << 999.9; // test
    21. }
    22. }
    23. for (int i=0; i<FILES; i++)
    24. {
    25. v.at(i)->device()->close();
    26. }
    To copy to clipboard, switch view to plain text mode 


    This compiled, but when it ran, the debugger stopped in qglobal.h at a line having something to do with QFlags. This is the line where it seems to break in qglobal.h:

    inline QFlags operator&(Enum f) const { QFlags g; g.i = i & f; return g; }


    I avoided this altogether by opening a QFile - QDataStream and writing values, then closing the stream each time within a loop. This worked but seems terribly inefficient.

    Can someone demonstrate how to open and write to multiple binary files using QDataStream's operators?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem using multiple QDataStreams

    You are creating files on the stack hence they are destroyed after you leave the iteration of the loop where they are created. Create them on heap as you do with QDataStreams.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    TheShow (24th November 2009)

Similar Threads

  1. problem while compiling qextserial port
    By jjbabu in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2007, 13:01
  2. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  3. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  4. Multiple decleration problem
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 21st May 2007, 09:07
  5. Replies: 10
    Last Post: 1st February 2006, 10:08

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
  •  
Qt is a trademark of The Qt Company.