Results 1 to 3 of 3

Thread: QFile and Creating Files

  1. #1
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    1

    Default QFile and Creating Files

    Ok first off I am a total noob at C++ and QT as I just started a few days ago, but I have been trudging through some online tutorials and just recently hit a snag. I am quite sure it's a simple issue, but here goes.

    Got a list widget, line edit widget and a button that when pressed outputs the text to the list widget and then outputs to a file. Now I got it working great as long as I hard coded the filename into my program, but when I wanted to possibly create a new file I could not figure out where to put it.

    You can see below where I wanted to put it, but I knew it would not work since it was encapsulated by the if statement. I think my brain is fried from looking at all this for too long hehe.

    Qt Code:
    1. void DialogImpl::doSomething() {
    2. QString linedt = lineEdit->text(); // Set linedt to the text in the Line Edit box
    3. if (!file.isOpen()) && ( linedt != "" ) { // If file is not open then open it
    4. QFile file("/home/jingoism/" + linedt); // This is wrong, and the part I dont understand
    5. QTextStream out(&file); // Also wrong, should go with QFile
    6. file.open(QIODevice::WriteOnly | QIODevice::Text); // Open the file as Writeonly
    7. listWidget->addItem("Opened File!"); // Verify in listWidget file is opened
    8. listWidget->addItem(linedt); // Add the line of text to the listWidget
    9. out << linedt << endl; // Add the line to the file
    10. lineEdit->clear(); // Clear the LineEdit box
    11. } else {
    12. if ( linedt != "" ) { // If text in the Line Edit box is not null then proceed
    13. out << linedt << endl;
    14. listWidget->addItem(linedt);
    15. lineEdit->clear();
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile and Creating Files

    Hey, is this compiling at all?
    You got it all wrong and you are right with some things .
    You can do without QTextStream
    QFile inherits QIODevice which has lower level methods for reading/writing files.

    Your logic in writing this program is not correct.
    Qt Code:
    1. void DialogIml::DialogImpl(...)
    2. {
    3. mFile = new QFile(somePath); //sice you require constatnt access to the file, it should be a member in your class.
    4. mFile->open(QIODevice::WriteOnly); //this should create the file if it does not exist
    5. //be careful because that could return false - if you lack permissions,etc
    6. }
    7.  
    8. void DialogImpl::~DialogImpl()
    9. {
    10. if(mFile)
    11. {
    12. mFile->close();
    13. delete mFile;
    14. }
    15. }
    16.  
    17. void DialogImpl::DoSomething()
    18. {
    19. QString linedt = lineEdit->text();
    20. if(mFile->isOpen())
    21. {
    22. mFile->write(linedt.ascii());//Writes a QByteArray to the file.
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Should be more or less like this.

    Regards

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

    Jingoism (28th July 2007)

  4. #3
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    1

    Default Re: QFile and Creating Files

    Ahh thats exactly what I was looking for. I knew the answer was probably fairly simple, but I could not seem to grasp how I would obtain constant access to the file. Of course now it seems really simple and I feel like a tool... hehe.

    Thank you for the quick response though as I am thoroughly enjoying QT.

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.