Results 1 to 3 of 3

Thread: How to write in a QFile

  1. #1
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt3 Qt4 Qt/Embedded

    Default How to write in a QFile

    Hello I want to write a text in a QFile , for example /home/hello.txt.
    Does anybody know any example of code for doing it ?
    Thanks
    Gorka

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to write in a QFile

    Yes, I do. And also the documentation. Search it!

  3. #3
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write in a QFile

    here .. try this one:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString file = "D:/hello.txt";
    4. QFile outputFile(file);
    5. outputFile.open(QIODevice::WriteOnly);
    6.  
    7. QString str = "text inside file";
    8.  
    9. if(!outputFile.isOpen())
    10. {
    11. //alert that file did not open
    12. }
    13.  
    14. QTextStream outStream(&outputFile);
    15.  
    16. outStream << str;
    17.  
    18. outputFile.close();
    19.  
    20. //open text file to user
    21. QDesktopServices::openUrl(QUrl(tr("file:///D:/hello.txt")));
    22.  
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Read and Write file to/from QFile
    By ruben.rodrigues in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2011, 14:57
  2. Replies: 5
    Last Post: 25th January 2011, 11:06
  3. QFile write isn't stable
    By ruben.rodrigues in forum Newbie
    Replies: 2
    Last Post: 8th July 2010, 21:14
  4. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  5. Unable to write to file QFile
    By cuter in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 11:19

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.