Results 1 to 8 of 8

Thread: How to return a QFile ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a QFile ?

    Keep in mind that this solution may lead to memory leak:
    Qt Code:
    1. QFile *MyClass::selectFile();
    2.  
    3. //...
    4.  
    5. QFile * file = selectFile();
    6. if (file) {
    7. doSomethingWithFile(file);
    8. delete file;
    9. }
    To copy to clipboard, switch view to plain text mode 
    If "dosomethingWithFile()" throws, control will never reach "delete file" and the resource will leak.
    So I don't really understand why you won't use a QString object:
    Qt Code:
    1. QStrign MyClass::selectFile();
    2.  
    3. //...
    4.  
    5. QString filePath = selectFile();
    6. if (filePath.isEmpty() == false) {
    7. QFile file(filePath);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Or at least use QFile pointer wrapped in smart pointer class, for example QSharedPointer<QFile>.

    // edit:
    btw. nDorian's code will cause a memory leak if selectFile() is called two or more times in a row without deleting "myFile" pointer in between. I know that this is forum section for "newbies" but this is important, any C++ programmer have to pay attention to such details - I know you are "not very experienced", but the sooner the better
    Last edited by stampede; 7th February 2014 at 10:16.

  2. The following user says thank you to stampede for this useful post:

    oldFox64 (7th February 2014)

  3. #2
    Join Date
    Feb 2014
    Posts
    15
    Thanks
    3

    Default Re: How to return a QFile ?

    I wanted to use QFile object because I thought is more understandable and "natural". But yes, you're right, I'm going to use a QString better.

  4. #3
    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: How to return a QFile ?

    QFile is the class that does data I/O.
    If you only need a reference to the file use either QString or QFileInfo.

    If you need the file, e.g. when you function does more than just create the instance, then return a smart pointer

    Qt Code:
    1. QSharedPointer<QFile> MyClass:getFile()
    2. {
    3. // ....
    4.  
    5. QSharedPointer<QFile> file(new QFile(filename);
    6. file->open(...); // or other tasks with file
    7.  
    8. return file;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 6th May 2013, 08:06
  2. Replies: 1
    Last Post: 2nd January 2013, 09:48
  3. Replies: 4
    Last Post: 9th May 2011, 09:52
  4. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  5. Slot with a return value?
    By lalesculiviu in forum Qt Programming
    Replies: 9
    Last Post: 21st December 2009, 06:27

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.