简体   繁体   中英

QT: how to save a generated file by the program into user's machine

I have a PyQt program which takes a file from user to be processed which then create a new file accordingly. i want the user to be able to download/save the new created file into their machine/desktop...

this is the uploading code:

def open_dialog_box(self):
    #To just print the name of file
     filename = QFileDialog.getOpenFileName()
     filename = filename[0]
     basename = os.path.basename(filename)

def pushButton_2_handler(self):
    path = QFileDialog.getOpenFileName()
    path = path[0]
    basename = os.path.basename(path)
    filename, file_extension = os.path.splitext(basename)
    print(path)
    print(filename)
    print(file_extension)
    self.textBrowser_fileSelectedName.clear()
    self.textBrowser_fileSelectedName.append(str(basename))

You want to use another QFileDialog but this one will be setup to return the selected folder/custom name the user typed.

def save_file(self):

    # Open a dialog so that the user can select a target location.
    dialog = QFileDialog()
    dialog.setWindowTitle("Select a location to save your file.")
    dialog.setAcceptMode(QFileDialog.AcceptSave)
    dialog.exec_()
    if not dialog.selectedFiles(): # If the user closed the choice window without selecting anything.
        return
    else:
        path_to_file = dialog.selectedFiles()[0]

    # Create that file and work on it, for example:
    with open(path_to_file + ".txt") as file:
        file.write("Hello there!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM