Results 1 to 8 of 8

Thread: How to return a QFile ?

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

    Default How to return a QFile ?

    Sry for the dumb question, but I'm not very experienced with C++ and I don't manage find the appropriate return type of the following function:

    Qt Code:
    1. ?? MyClass::selectFile(){
    2. const QString fileName = QFileDialog::getOpenFileName(this,
    3. tr("Select file"), qgetenv("HOMEPATH")+"\\Desktop", tr("File(*.hex)"));
    4.  
    5. if(fileName==NULL)
    6. return NULL;
    7.  
    8. QFile file(fileName);
    9.  
    10. return ??;
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2011
    Location
    Poland
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to return a QFile ?

    Helo! You can't return QFile object, because it don't have public copy constructor. It is possoble to return reference or pointer to QFile object but in these case it must be a member of your class (object created in function will be destroyed at the end of function class member not).

  3. #3
    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 ?

    Why not
    Qt Code:
    1. QString MyClass::selectFile() {
    2. return QFileDialog::getOpenFileName(this, tr("Select file"), qgetenv("HOMEPATH")+"\\Desktop", tr("File(*.hex)"));
    3. }
    To copy to clipboard, switch view to plain text mode 
    ?
    Last edited by stampede; 7th February 2014 at 09:19.

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

    Default Re: How to return a QFile ?

    Didn't know that, thank you.

    So to return a QFile pointer, how it should be?

  5. #5
    Join Date
    Jun 2011
    Location
    Poland
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to return a QFile ?

    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. MyClass();
    5.  
    6.  
    7. QFile *selectFile();
    8.  
    9. private:
    10. QFile *myFile;
    11. };
    12.  
    13. MyClass::MyClass()
    14. {
    15. }
    16.  
    17. QFile *MyClass::selectFile()
    18. {
    19. const QString fileName = "abc";
    20.  
    21. if(fileName==NULL)
    22. return NULL;
    23.  
    24. myFile = new QFile(fileName);
    25.  
    26. return myFile;
    27. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to nDorian for this useful post:

    oldFox64 (7th February 2014)

  7. #6
    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.

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

    oldFox64 (7th February 2014)

  9. #7
    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.

  10. #8
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.