Results 1 to 4 of 4

Thread: How to prevent saving when files are opened by another process

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to prevent saving when files are opened by another process

    I currently have an application that opens several .csv files and stores them in temporary holders (QHash, QList...). The problem I have seen is that when the user clicks the 'Save' option I try and open the files and save all the information contained in the temporary objects to the corresponding .csv files. If the user has one of those files open in say Microsoft excel how do I control the way that event is handled? Is there a special function in QFile or QTextStream that I can call to check this status? Right now very strange behavior happens because one file might get saved while the others aren't so the files are no longer in sync and the application just crashes which is obviously not a desired behavior.

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

    Default Re: How to prevent saving when files are opened by another process

    Is your problem multiple different files not saving at the same time? Or is it multiple 'handles' on a single file becoming out of sync?

  3. #3
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to prevent saving when files are opened by another process

    Multiple files not saving at the same time, currently these file names are kept in a QList<QString> and then read in one at a time when attempting to save. Thus if one file is open in e.g. Excel then the saving process gets thrown out of whack. I realize that this could be an improper way of saving multiple files and if so please feel free to suggest a different route based on correctness, however I still would like to know if it is possible to do what I initially asked. Thanks again for any help!

  4. #4
    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 prevent saving when files are opened by another process

    I am assuming the problem arises because Excel locks one or more of the files.

    Attempt to open open all three files for QIODevice::ReadWrite and check that they opened successfully before truncating and writing anything. The files that can be opened should not be truncated by this, and the files that cannot be opened should cause you to not write anything.

    Qt Code:
    1. QFile a("a.txt");
    2. QFile b("b.txt");
    3. QFile c("c.txt");
    4.  
    5. if (a.open(QFile::ReadWrite) && b.open(QFile::ReadWrite) && c.open(QFile::ReadWrite)) {
    6. qDebug() << "All opened";
    7. a.resize(0);
    8. QTextStream as(&a);
    9. as << QDateTime::currentDateTime().toString() << endl;
    10.  
    11. QTextStream bs(&b);
    12. b.resize(0);
    13. bs << QDateTime::currentDateTime().toString() << endl;
    14.  
    15. QTextStream cs(&c);
    16. c.resize(0);
    17. cs << QDateTime::currentDateTime().toString() << endl;
    18. }
    19. else
    20. qDebug() << "Not opened";
    21. a.close();
    22. b.close();
    23. c.close();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Saving / Loading Files
    By nightroad in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2011, 13:13
  2. Prevent saving files to System folders
    By ScottBrady in forum Newbie
    Replies: 8
    Last Post: 13th August 2011, 15:19
  3. Problem when saving bmp files
    By cdlaweed in forum Newbie
    Replies: 3
    Last Post: 17th March 2011, 09:20
  4. Implementing an "open recently opened files" menu?
    By joelthelion in forum Qt Programming
    Replies: 3
    Last Post: 6th March 2009, 10:49
  5. Saving already opened image ...
    By Godlike in forum Newbie
    Replies: 14
    Last Post: 28th March 2006, 21:17

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.