Results 1 to 14 of 14

Thread: How to create in memory QFile

  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    22
    Thanks
    3
    Qt products
    Qt4

    Default How to create in memory QFile

    Hi,
    I want to create a QFile that currently resides in memory. This is required as i want to add it as a resource to a QTextDocument.
    On some user action i want to write the QTextDocument and in the same directory i also want the QFile created in memory to be written.
    Please provide some pointers.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to create in memory QFile

    Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,

    Qt Code:
    1. QFile *file;
    2. file=new QFile("demo.txt");.
    3. if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
    4. return;
    5.  
    6. QTextStream out(file);
    7. out << "The magic number is: " << 49 << "\n";
    8.  
    9. file->close(); //for closing the file.
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by sonulohani View Post
    Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,

    Qt Code:
    1. QFile *file;
    2. file=new QFile("demo.txt");.
    3. if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
    4. return;
    5.  
    6. QTextStream out(file);
    7. out << "The magic number is: " << 49 << "\n";
    8.  
    9. file->close(); //for closing the file.
    To copy to clipboard, switch view to plain text mode 
    contains leaks...

    Why put QFile on the heap at all?

    If op wants to write/print QTextDocument, then he should familiarise himself with http://doc.qt.io/qt-4.8/qtextdocumentwriter
    Last edited by amleto; 2nd August 2012 at 13:37.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Mar 2012
    Location
    India
    Posts
    22
    Thanks
    3
    Qt products
    Qt4

    Default Re: How to create in memory QFile

    I am using QTextDocumentWriter to write my QTextDocument, but here along with the document i want to write a file and add a link to the file in the document. I have added the link and it is working, but i want to write the document or not depends on whether user does export. For that I want to add the file to the document resource using the QTextDocument::addResource which will hold the QFile so that i could get the file when i want to write it.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create in memory QFile

    QFile doesnt actually hold the data of the file so not sure that will help you much. You might as well just be saving the file name...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jul 2012
    Posts
    123
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by sonulohani View Post
    Ok. For that, i think dont add the file in the resource folder,except that, directly create it in your project folder and access it from there. Because if you put your file in the resource and it will get compile and you'll not able to edit it. For that,

    Qt Code:
    1. QFile *file;
    2. file=new QFile("demo.txt");.
    3. if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
    4. return;
    5.  
    6. QTextStream out(file);
    7. out << "The magic number is: " << 49 << "\n";
    8.  
    9. file->close(); //for closing the file.
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. file->flush();
    To copy to clipboard, switch view to plain text mode 
    flush is very important when you write to the file Sometime can bad things happen when you don't flush it

  7. #7
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to create in memory QFile

    Who said that QFile should always be on stack? Its depend on you whether you want to allocate in stack or heap.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create in memory QFile

    QBuffer is the in-memory QIODevice you might want.

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by sonulohani View Post
    Who said that QFile should always be on stack? Its depend on you whether you want to allocate in stack or heap.
    nobody said that.

    If, however, you are going to write an example that looks like a java programmer wrote it, perhaps you should expect some comeback.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  10. #10
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to create in memory QFile

    Then do coding in java, why r u here? Its depend on your program methodology. And if someone is here than atleast he should know the basic concept of c++(memory allocation). You shouldnt have to think about what they do with this code or if he /she will understand this code or not. Okay, point out the line that is causing memory leak in my code.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  11. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by sonulohani View Post
    Okay, point out the line that is causing memory leak in my code.
    Qt Code:
    1. ...
    2. if (!file->open(QIODevice::WriteOnly | QIODevice::Text))
    3. return; //memoy leak if a file can't be opened
    4. ...
    To copy to clipboard, switch view to plain text mode 
    The simplest way to fix this without using delete it's just provide a parent in QFile's ctor.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create in memory QFile

    No, simplest way is to say `QFile file;`, not `QFile* file = new ...`
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by amleto View Post
    No, simplest way is to say `QFile file;`, not `QFile* file = new ...`
    Yup, and change all "->" to "." if you have tons of code.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #14
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create in memory QFile

    Quote Originally Posted by sonulohani View Post
    Then do coding in java, why r u here? Its depend on your program methodology. And if someone is here than atleast he should know the basic concept of c++(memory allocation). You shouldnt have to think about what they do with this code or if he /she will understand this code or not. Okay, point out the line that is causing memory leak in my code.
    You are the one writing like java: Allocating on the heap when only a local variable is needed - check. Using new without delete - check.

    Your leak has been pointed out already, although why you couldn't see it for yourself is concerning.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  2. Replies: 3
    Last Post: 26th March 2010, 07:26
  3. Replies: 5
    Last Post: 27th May 2009, 12:49
  4. create Memory-mapped file
    By weixj2003ld in forum Newbie
    Replies: 2
    Last Post: 17th April 2009, 10:47
  5. how to create html file in memory
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2007, 09:30

Tags for this Thread

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.