Results 1 to 2 of 2

Thread: Creating QFile Objects in a loop

  1. #1
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Creating QFile Objects in a loop

    Hi All,

    I have a quick question:

    I have a dir which has a huge number of files in it. I need to iterate through all the files and open them.

    Qt Code:
    1. while (qdirIterator.hasNext())
    2.  
    3. {
    4. QString fileName = qdirIterator.next();
    5. QFileInfo fileInfo = qdirIterator.fileInfo();
    6.  
    7. QFile inFile( fileInfo.absoluteFilePath() );
    8.  
    9. if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
    10. {
    11. qCritical( "Failed to open file for reading." );
    12. return;
    13. }
    14. .
    15. .
    16. .
    17. } // while
    To copy to clipboard, switch view to plain text mode 

    First up when I look at this code I feel I am constructing too many QFile objects. So I want to do the following:

    Qt Code:
    1. QFile inFile;
    2. while (qdirIterator.hasNext())
    3. {
    4. QString fileName = qdirIterator.next();
    5. QFileInfo fileInfo = qdirIterator.fileInfo();
    6.  
    7. if( !inFile.open( fileName, QIODevice::ReadOnly | QIODevice::Text ) )
    8. {
    9. qCritical( "Failed to open file for reading." );
    10. return;
    11. }
    12. .
    13. .
    14. .
    15. inFile.close();
    16. } // while
    To copy to clipboard, switch view to plain text mode 

    Problems with this are:

    1. "QFile:: open" does not take QString as an argument.
    2. And even if I manage to use "QFile:: open" in this manner the docs say, "When a QFile is opened using this function, close() does not actually close the file, but only flushes it."

    So is there a workaround for 1. and also make sure that "QFile:: close()" actually closes the file and not just flush it?

    Thanks in Advance,
    sky.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Creating QFile Objects in a loop

    Quote Originally Posted by sky View Post
    First up when I look at this code I feel I am constructing too many QFile objects.
    You're only constructing one file at a time.
    The QFile will go out of scope (destroyed) every iteration.

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

    sky (3rd December 2010)

Similar Threads

  1. QtScript and creating non QObject based objects
    By tbscope in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2011, 08:20
  2. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43
  3. creating/deleting Objects // memory leaks
    By janus in forum Qt Programming
    Replies: 4
    Last Post: 27th March 2008, 18:17
  4. QFile and Creating Files
    By Jingoism in forum Newbie
    Replies: 2
    Last Post: 28th July 2007, 17:11
  5. creating objects in a for()
    By phil_ in forum Newbie
    Replies: 3
    Last Post: 21st January 2006, 13:16

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.