Results 1 to 5 of 5

Thread: Reuse saved file name

  1. #1
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Reuse saved file name

    Hello! I'm new to Qt designer and programming. I tried to create a file (e.g. "sample") using the below function and want to autosave the file every x minutes using 'timer'. But each time I am getting a prompt to select a new file. My question: How can I reuse the same file i.e. "sample" without getting prompts and save data in it.

    Qt Code:
    1. def sampleSave(self):
    2. QtWidgets.QFileDialog.getSaveFileName(caption="Choose file to save configuration")
    3.  
    4. timer = threading.Timer(300, self.sampleSave)
    5. timer.start()
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 10th August 2021 at 17:18. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,102
    Thanks
    289
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reuse saved file name

    You need two separate methods: one to get the file name the first time, and another that you use with the QTimer to autosave. When you start the program, the "save file" name is blank. You start the timer, the timeout slot looks at the name and see that it is empty, so it calls the method to get the file name from the user. It then saves the name in a member variable. Then, each time the timeout occurs, it sees that the filename is not blank and uses that name to save instead of asking for a new one.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reuse saved file name

    Hi there! Thanks for the response. However, since I'm at a beginner level in programming, could you direct me to a resource where I can get this in detail with an example?

    I am using the function as:

    Qt Code:
    1. def sampleSave(self):
    2. filename = QtWidgets.QFileDialog.getSaveFileName(caption="Choose pkl to save configuration")
    To copy to clipboard, switch view to plain text mode 

    Now, how do I recall this filename?

    Qt Code:
    1. timer = threading.Timer(300, self.sampleSave, filename)
    2. timer.start()
    To copy to clipboard, switch view to plain text mode 

    is this correct?
    Last edited by meng; 11th August 2021 at 16:03.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,102
    Thanks
    289
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reuse saved file name

    I am not a Python expert, but your code should look something like this:

    Qt Code:
    1. def __init__( self ) :
    2. self.filename = ""
    3.  
    4. def getAutosaveName(self):
    5. self.filename = QtWidgets.QFileDialog.getSaveFileName(caption="Choose pkl to save configuration")
    6.  
    7. def onTimeout( self ) :
    8. if self.filename.isEmpty() :
    9. getAutosaveName()
    10.  
    11. # use self.filename to save the file
    12.  
    13.  
    14. timer = threading.Timer( 300, self.onTimeout )
    15. timer.start()
    To copy to clipboard, switch view to plain text mode 

    What units are the first argument in threading.Timer()? Seconds or milliseconds? You certainly don't want to be autosaving a file every 300 ms - if you did, your program wouldn't have time to do anything else. Saving every 300 seconds (300000 ms) would be more reasonable.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    meng (11th August 2021)

  6. #5
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reuse saved file name

    It is in seconds. Looking at the code you mentioned, it is more understandable to me now. Thank you!

Similar Threads

  1. Automatic file extension is not added to saved file on mac os x
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 06:56
  2. reuse QtDesigner?
    By nicolas1 in forum Qt Programming
    Replies: 5
    Last Post: 22nd March 2009, 14:00
  3. QPainter reuse within a paintEvent
    By Micawber in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2008, 16:51
  4. can't reuse a pure c++ .h file's methods and classes
    By sincnarf in forum General Programming
    Replies: 11
    Last Post: 3rd August 2007, 03:39
  5. Reuse QT code in QTEmbedded
    By the_bis in forum Newbie
    Replies: 3
    Last Post: 8th September 2006, 18:03

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.