Results 1 to 4 of 4

Thread: Using QTimer to install an autosave feature

  1. #1
    Join Date
    Jan 2017
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QTimer to install an autosave feature

    I want to install an autosave feature and was considering using QTimer. I looked at the docs, but most are in C, so I am having trouble getting it correct. So far, I have:
    Qt Code:
    1. timer = QtCore.QTimer()
    2. timer.timeout(self.savefile)
    3. timer.start(600000)
    To copy to clipboard, switch view to plain text mode 

    1. Is QTimer the way to get what I want? If so, what is the correct code (Line 2 above is not correct)
    2. If QTimer is the way to get what I want, where does the above code go? in the init of the module that contains self.savefile? If not, where?
    3. If QTimer is not correct, what is the better way?
    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QTimer to install an autosave feature

    1. Is QTimer the way to get what I want? If so, what is the correct code (Line 2 above is not correct)
    Seems reasonable. You need to connect the timeout signal correctly (line 2) and probably make sure that savefile only does something if the "document" is dirty.
    2. If QTimer is the way to get what I want, where does the above code go? in the init of the module that contains self.savefile? If not, where?
    Create the timer and connect it in the constructor/init.
    Start the timer when a "document" is opened and stop it when the "document" is closed.[/LIST]
    3. If QTimer is not correct, what is the better way?
    You could call savefile() when the "document" is modified and and clear the modified flag when it is saved. This could impose an unacceptable load depending on how modifications occur.

    Example opens a document and closes it 5 seconds later, with a save every 2 seconds. App will terminate after 10 seconds.
    Qt Code:
    1. #!/usr/bin/env python3
    2.  
    3. import sys
    4. from PyQt5.QtCore import QCoreApplication, QObject, QTimer
    5.  
    6. class Foo(QObject):
    7. def __init__(self, *args, **kwargs):
    8. super(Foo, self).__init__(*args, **kwargs)
    9. self.autosaveTimer = QTimer()
    10. self.autosaveTimer.setInterval(2000)
    11. self.autosaveTimer.timeout.connect(self.saveFile)
    12.  
    13. def openFile(self):
    14. # Open the document
    15. print ("Open...")
    16. self.autosaveTimer.start()
    17.  
    18. def closeFile(self):
    19. # Close the document
    20. print ("Close...")
    21. self.autosaveTimer.stop()
    22.  
    23. def saveFile(self):
    24. # Save the document if it is dirty
    25. print ("Saving...")
    26.  
    27. if __name__ == '__main__':
    28. app = QCoreApplication(sys.argv)
    29. QTimer.singleShot(10000, app.quit)
    30.  
    31. foo = Foo()
    32. foo.openFile()
    33. QTimer.singleShot(5000, foo.closeFile)
    34.  
    35. app.exec_();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2017
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QTimer to install an autosave feature

    One last question. Does autosaving interfere with anything else going on within the program, like if the program is running some loop, or whether the user is in the process of entering data in the UI or whether the user just manually clicked the same "Save" button?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QTimer to install an autosave feature

    Quote Originally Posted by dennisvz View Post
    One last question. Does autosaving interfere with anything else going on within the program, like if the program is running some loop, or whether the user is in the process of entering data in the UI or whether the user just manually clicked the same "Save" button?
    In a straightforward single-threaded Qt program the timer will go off, and the auto-save called, only when the program returns to the Qt event loop. If your program is busy doing some processing that does not return to the event loop, e.g. a 5 minute computation, then it will not be interrupted. While the save is occurring the program not be doing anything else, which can be a problem if that takes a long time.

    You probably also need to be defensive and auto-save to somewhere other than the real file, and only overwrite the real file if the user selects Save deliberately. On file open look for the matching auto-save and, if found (because the program crashed last time), offer to use that instead. Depends on your application though.

Similar Threads

  1. Feature request
    By raphael.lencrerot in forum Qwt
    Replies: 4
    Last Post: 17th April 2013, 14:13
  2. Replies: 1
    Last Post: 25th October 2012, 19:47
  3. Replies: 15
    Last Post: 4th August 2012, 19:11
  4. Replies: 6
    Last Post: 13th March 2011, 23:49
  5. Autosave in a spinbox delegate
    By SailinShoes in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2008, 07:20

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.